下面列出了java.util.zip.Inflater#getRemaining ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Read compressed data
*
* @param hoff header offset
* @param doff data offset
* @return the array of data
*/
public byte[] readCompData(long hoff, long doff) throws IOException {
int numin; /* # input bytes processed */
long pos = 0;
long len = raf.length();
raf.seek(pos);
numin = (int) (len - hoff);
// Read in the contents of the NEXRAD Level III product header
// nids header process
byte[] b = new byte[(int) len];
raf.readFully(b);
/* a new copy of buff with only compressed bytes */
// byte[] comp = new byte[numin - 4];
// System.arraycopy( b, (int)hoff, comp, 0, numin -4 );
// decompress the bytes
Inflater inf = new Inflater(false);
int resultLength;
int result = 0;
// byte[] inflateData = null;
byte[] tmp;
int uncompLen = 24500; /* length of decompress space */
byte[] uncomp = new byte[uncompLen];
inf.setInput(b, (int) hoff, numin - 4);
int limit = 20000;
while (inf.getRemaining() > 0) {
try {
resultLength = inf.inflate(uncomp, result, 4000);
} catch (DataFormatException ex) {
logger.error("ERROR on inflation ", ex);
throw new IOException(ex.getMessage());
}
result = result + resultLength;
if (result > limit) {
// when uncomp data larger then limit, the uncomp need to increase size
tmp = new byte[result];
System.arraycopy(uncomp, 0, tmp, 0, result);
uncompLen = uncompLen + 10000;
uncomp = new byte[uncompLen];
System.arraycopy(tmp, 0, uncomp, 0, result);
}
if (resultLength == 0) {
int tt = inf.getRemaining();
byte[] b2 = new byte[2];
System.arraycopy(b, (int) hoff + numin - 4 - tt, b2, 0, 2);
if (headerParser.isZlibHed(b2) == 0) {
System.arraycopy(b, (int) hoff + numin - 4 - tt, uncomp, result, tt);
result = result + tt;
break;
}
inf.reset();
inf.setInput(b, (int) hoff + numin - 4 - tt, tt);
}
}
inf.end();
int off;
off = 2 * (((uncomp[0] & 0x3F) << 8) | (uncomp[1] & 0xFF));
/* eat WMO and PIL */
for (int i = 0; i < 2; i++) {
while ((off < result) && (uncomp[off] != '\n'))
off++;
off++;
}
byte[] data = new byte[(int) (result - off - doff)];
// byte[] hedata = new byte[(int)doff];
// System.arraycopy(uncomp, off, hedata, 0, (int)doff);
System.arraycopy(uncomp, off + (int) doff, data, 0, result - off - (int) doff);
return data;
}
byte[] readPIB(ucar.unidata.io.RandomAccessFile raf) throws IOException {
int pos = 0;
raf.seek(pos);
// gini header process
byte[] b = new byte[GINI_PIB_LEN + GINI_HED_LEN];
byte[] buf = new byte[GINI_HED_LEN];
byte[] head = new byte[GINI_PDB_LEN];
raf.readFully(b);
String pib = new String(b, StandardCharsets.UTF_8);
pos = findWMOHeader(pib);
dataStart = pos + GINI_PDB_LEN;
// Test the next two bytes to see if the image portion looks like
// it is zlib-compressed
byte[] b2 = {b[pos], b[pos + 1]};
int pos1 = 0;
if (Giniiosp.isZlibHed(b2)) {
Z_type = 1;
Inflater inflater = new Inflater(false);
inflater.setInput(b, pos, GINI_HED_LEN);
try {
int resultLength = inflater.inflate(buf, 0, GINI_HED_LEN);
if (resultLength != GINI_HED_LEN)
log.warn("GINI: Zlib inflated image header size error");
} catch (DataFormatException ex) {
log.error("ERROR on inflation " + ex.getMessage());
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
int inflatedLen = GINI_HED_LEN - inflater.getRemaining();
String inf = new String(buf, StandardCharsets.UTF_8);
pos1 = findWMOHeader(inf);
System.arraycopy(buf, pos1, head, 0, GINI_PDB_LEN);
dataStart = pos + inflatedLen;
} else {
System.arraycopy(b, pos, head, 0, GINI_PDB_LEN);
}
if (pos == 0 && pos1 == 0) {
throw new IOException("Error on Gini File");
}
return head;
}
private Array readCompressedZlib(ucar.nc2.Variable v2, long dataPos, int nx, int ny, List<Range> ranges, int[] levels)
throws IOException, InvalidRangeException {
// Get to the proper offset and read in the rest of the compressed data
raf.seek(dataPos);
int data_size = (int) (raf.length() - dataPos); // or 5120 as read buffer size
byte[] data = new byte[data_size];
raf.readFully(data);
// Buffer for decompressing data
byte[] uncomp = new byte[nx * ny];
int offset = 0;
// Set-up zlib decompression (inflation)
Inflater inflater = new Inflater(false);
inflater.setInput(data);
// Loop while the inflater has data and we have space in final buffer
// This will end up ignoring the last few compressed bytes, which
// correspond to the end of file marker, which is a single row of pixels
// of alternating 0/255.
while (inflater.getRemaining() > 0 && offset < uncomp.length) {
// Try to decompress what's left, which ends up decompressing one block
try {
offset += inflater.inflate(uncomp, offset, uncomp.length - offset);
} catch (DataFormatException ex) {
throw new IOException(ex);
}
// If the last block finished...
if (inflater.finished()) {
// See if anything's left
int bytesLeft = inflater.getRemaining();
if (bytesLeft > 0) {
// Figure out where we are in the input data
int inputOffset = data_size - bytesLeft;
// Check if remaining data are zlib--if not copy out and bail
byte[] b2 = new byte[2];
System.arraycopy(data, inputOffset, b2, 0, b2.length);
if (!isZlibHed(b2)) {
System.arraycopy(data, inputOffset, uncomp, offset, bytesLeft);
break;
}
// Otherwise, set up for decompressing next block
inflater.reset();
inflater.setInput(data, inputOffset, bytesLeft);
}
}
}
inflater.end();
// Turn the decompressed data into an array, caching as appropriate
Array array = makeArray(uncomp, levels, v2.getShape());
if (levels == null && array.getSize() < Variable.defaultSizeToCache)
v2.setCachedData(array, false);
return array.sectionNoReduce(ranges);
}
public Array readCompressedZlib(ucar.nc2.Variable v2, long dataPos, int nx, int ny, int[] origin, int[] shape,
int[] stride) throws IOException, InvalidRangeException {
long length = raf.length();
raf.seek(dataPos);
int data_size = (int) (length - dataPos); // or 5120 as read buffer size
byte[] data = new byte[data_size];
raf.readFully(data);
// decompress the bytes
int resultLength;
int result = 0;
byte[] tmp;
int uncompLen; /* length of decompress space */
byte[] uncomp = new byte[nx * (ny + 1) + 4000];
Inflater inflater = new Inflater(false);
inflater.setInput(data, 0, data_size);
int offset = 0;
int limit = nx * ny + nx;
while (inflater.getRemaining() > 0) {
try {
resultLength = inflater.inflate(uncomp, offset, 4000);
} catch (DataFormatException ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
offset = offset + resultLength;
result = result + resultLength;
if ((result) > limit) {
// when uncomp data larger then limit, the uncomp need to increase size
tmp = new byte[result];
System.arraycopy(uncomp, 0, tmp, 0, result);
uncompLen = result + 4000;
uncomp = new byte[uncompLen];
System.arraycopy(tmp, 0, uncomp, 0, result);
}
if (resultLength == 0) {
int tt = inflater.getRemaining();
byte[] b2 = new byte[2];
System.arraycopy(data, data_size - tt, b2, 0, 2);
if (isZlibHed(b2) == 0) {
System.arraycopy(data, data_size - tt, uncomp, result, tt);
break;
}
inflater.reset();
inflater.setInput(data, data_size - tt, tt);
}
}
inflater.end();
byte[] inflateData = new byte[nx * ny];
System.arraycopy(uncomp, 0, inflateData, 0, nx * ny);
Array array = Array.factory(DataType.BYTE, v2.getShape(), inflateData);
if (array.getSize() < Variable.defaultSizeToCache)
v2.setCachedData(array, false);
return array.sectionNoReduce(origin, shape, stride);
}