1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/** Copyright (c) 2010 Scott A. Crosby. <scott@sacrosby.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package crosby.binary.file;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import crosby.binary.Fileformat;
/**
* Stores the position in the stream of a fileblock so that it can be easily
* read in a random-access fashion.
*
* We can turn this into a 'real' block by appropriately seeking into the file
* and doing a 'read'.
*
* */
public class FileBlockPosition extends FileBlockBase {
protected FileBlockPosition(String type, ByteString indexdata) {
super(type, indexdata);
}
/** Parse out and decompress the data part of a fileblock helper function. */
FileBlock parseData(byte[] buf) throws InvalidProtocolBufferException {
FileBlock out = FileBlock.newInstance(type, null, indexdata);
Fileformat.Blob blob = Fileformat.Blob.parseFrom(buf);
if (blob.hasRaw()) {
out.data = blob.getRaw();
} else if (blob.hasZlibData()) {
byte[] buf2 = new byte[blob.getRawSize()];
Inflater decompresser = new Inflater();
decompresser.setInput(blob.getZlibData().toByteArray());
// decompresser.getRemaining();
try {
decompresser.inflate(buf2);
} catch (DataFormatException e) {
throw new UncheckedIOException(new FileFormatException(e));
}
assert (decompresser.finished());
decompresser.end();
out.data = ByteString.copyFrom(buf2);
}
return out;
}
public int getDatasize() {
return datasize;
}
/*
* Given any form of fileblock and an offset/length value, return a
* reference that can be used to dereference and read the contents.
*/
static FileBlockPosition newInstance(FileBlockBase base, long offset,
int length) {
FileBlockPosition out = new FileBlockPosition(base.type, base.indexdata);
out.datasize = length;
out.data_offset = offset;
return out;
}
public FileBlock read(InputStream input) throws IOException {
if (input instanceof FileInputStream) {
((FileInputStream) input).getChannel().position(data_offset);
byte[] buf = new byte[getDatasize()];
(new DataInputStream(input)).readFully(buf);
return parseData(buf);
} else {
throw new IllegalArgumentException("Random access binary reads require seekability");
}
}
/**
* TODO: Convert this reference into a serialized representation that can be
* stored.
*/
public ByteString serialize() {
throw new UnsupportedOperationException("TODO");
}
/** TODO: Parse a serialized representation of this block reference */
static FileBlockPosition parseFrom(ByteString b) {
throw new UnsupportedOperationException("TODO");
}
protected int datasize;
/** Offset into the file of the data part of the block */
long data_offset;
}
|