下面列出了java.util.zip.DataFormatException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public byte[] decompress(byte[] data){
Inflater decompresser = new Inflater();
decompresser.setInput(data);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] result = new byte[100000];
int length = 0;
try {
while (!decompresser.finished()) {
length = decompresser.inflate(result);
if (length > 0) {
os.write(result, 0, length);
} else {
break;
}
}
} catch (DataFormatException e) {
e.printStackTrace();
}
return os.toByteArray();
}
private void readCompressed(int plen, int dlen) throws IOException {
if (dlen >= getThreshold()) {
byte[] data = new byte[plen];
in.readFully(data, 0, plen);
Inflater inflater = new Inflater();
inflater.setInput(data);
byte[] uncompressed = new byte[dlen];
try {
inflater.inflate(uncompressed);
} catch (DataFormatException dataformatexception) {
dataformatexception.printStackTrace();
throw new IOException("Bad compressed data format");
} finally {
inflater.end();
}
ByteArrayDataInputWrapper buf = new ByteArrayDataInputWrapper(uncompressed);
int type = Packet.readVarInt(buf);
readPacket(dlen, type, buf);
} else {
throw new IOException("Data was smaller than threshold!");
}
}
public static byte[] uncompress(byte[] input) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater();
try {
decompressor.setInput(input);
final byte[] buf = new byte[2048];
while (!decompressor.finished()) {
int count = 0;
try {
count = decompressor.inflate(buf);
} catch (DataFormatException e) {
e.printStackTrace();
}
bos.write(buf, 0, count);
}
} finally {
decompressor.end();
}
return bos.toByteArray();
}
@Override
public byte[] uncompress(byte[] data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater();
try {
decompressor.setInput(data);
final byte[] buf = new byte[2048];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
} catch (DataFormatException e) {
e.printStackTrace();
} finally {
decompressor.end();
}
return bos.toByteArray();
}
/** Inflate the given byte buffer into this VEXBlock's data field. */
private void inflate (byte[] input) {
data = new byte[BUFFER_SIZE];
int pos = 0;
Inflater inflater = new Inflater();
inflater.setInput(input, 0, input.length);
try {
while (!inflater.finished()) {
pos += inflater.inflate(data, pos, data.length - pos);
}
} catch (DataFormatException e) {
e.printStackTrace();
pos = 0;
}
inflater.end();
nBytes = pos;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress byte[]
* @return byte[]
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput(bytesToDecompress, 0, numberOfBytesToDecompress);
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<>();
try {
while (!inflater.needsInput()) {
byte[] bytesDecompressedBuffer = new byte[numberOfBytesToDecompress];
int numberOfBytesDecompressedThisTime = inflater.inflate(bytesDecompressedBuffer);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
public static byte[] unzip(byte[] rawData) {
//seperate the first byte into data and flag
byte flag = rawData[0];
if(flag == FLAG_COMPRESSED) {
Inflater inflater = new Inflater();
inflater.setInput(rawData, 1, rawData.length - 1);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(rawData.length - 1);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = 0;
try {
count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
} catch (DataFormatException e) {
e.printStackTrace();
}
}
return outputStream.toByteArray();
}
else {
//data was not compressed, strip the first flag byte off the data
byte[] data = new byte[rawData.length - 1];
System.arraycopy(rawData, 1, data, 0, data.length);
return data;
}
}
private void checkMatrix() throws IOException {
System.out.println("Detecting whether binary matrix corresponds to SNP and Probe definition.");
long expectedsize = (long) snps.length * probes.length;
System.out.println("Expected matrix size:\t" + expectedsize + " Z-scores");
System.out.println("Checking matrix: ");
ProgressBar pb = new ProgressBar(snps.length);
long count = 0;
for (int i = 0; i < snps.length; i++) {
long index = snps[i].getzScoreIndex();
long next = -1;
if (i + 1 < snps.length) {
next = snps[i + 1].getzScoreIndex();
}
try {
bgfm.read(index, next, probes.length);
// for(int f=0; f<floats.length; f++){
// Float fs = floats[f];
// if(!Float.isNaN(fs)){
// if(fs > maxfloat){
// maxfloat = fs;
// }
// if(fs < minfloat){
// minfloat = fs;
// }
// }
// }
} catch (DataFormatException e) {
System.out.println("");
e.printStackTrace();
System.exit(-1);
}
pb.iterate();
}
pb.close();
System.out.println("");
System.out.println("All Probes are present");
System.out.println("Matrix is OK");
}
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;
}
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);
}
/**
* zlib decompress 2 byte
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
cached_rec_block prepareRecordBlock(record_info_struct RinfoI, int Rinfo_id) throws IOException {
if(RinfoI_cache_.blockID==Rinfo_id)
return RinfoI_cache_;
if(RinfoI==null)
RinfoI = _record_info_struct_list[Rinfo_id];
DataInputStream data_in = getStreamAt(_record_block_offset+_number_width*4+_num_record_blocks*2*_number_width+
RinfoI.compressed_size_accumulator);
int compressed_size = (int) RinfoI.compressed_size;
int decompressed_size = rec_decompressed_size = (int) RinfoI.decompressed_size;//用于验证
byte[] record_block_compressed = new byte[compressed_size];
//System.out.println(compressed_size) ;
//System.out.println(decompressed_size) ;
data_in.read(record_block_compressed);
data_in.close();
// 4 bytes indicates block compression type
//BU.printBytes(record_block_compressed,0,4);
// 4 bytes adler checksum of uncompressed content
//ByteBuffer sf1 = ByteBuffer.wrap(record_block_compressed);
//int adler32 = sf1.order(ByteOrder.BIG_ENDIAN).getInt(4);
cached_rec_block RinfoI_cache = new cached_rec_block();
//RinfoI_cache.blockLen=decompressed_size;
RinfoI_cache.blockOff=0;
//解压开始
switch (record_block_compressed[0]|record_block_compressed[1]<<8|record_block_compressed[2]<<16|record_block_compressed[3]<<32){
default:
case 0://no compression
RinfoI_cache.record_block_=record_block_compressed;
RinfoI_cache.blockOff=8;
//CMN.Log(_key_block_compressed.length,start,8);
//System.arraycopy(_key_block_compressed, (+8), key_block, 0,key_block.length);
break;
case 1:
RinfoI_cache.record_block_ = new byte[decompressed_size];
new LzoDecompressor1x().decompress(record_block_compressed, 8, compressed_size-8, RinfoI_cache.record_block_, 0,new lzo_uintp());
break;
case 2:
RinfoI_cache.record_block_ = new byte[decompressed_size];
//key_block = zlib_decompress(_key_block_compressed,(int) (start+8),(int)(compressedSize-8));
Inflater inf = new Inflater();
inf.setInput(record_block_compressed, +8, compressed_size-8);
try {
int ret = inf.inflate(RinfoI_cache.record_block_,0,decompressed_size);
} catch (DataFormatException e) {e.printStackTrace();}
break;
}
//CMN.show(record_block.length+"ss"+decompressed_size);
// notice not that adler32 return signed value
//assert(adler32 == (BU.calcChecksum(record_block,0,decompressed_size) ));
//assert(record_block.length == decompressed_size );
//当前内容块解压完毕
RinfoI_cache_=RinfoI_cache;
return RinfoI_cache;
}
public cached_key_block prepareItemByKeyInfo(key_info_struct infoI,int blockId,cached_key_block infoI_cache){
cached_key_block infoI_cache_ = this.infoI_cache_;
if(_key_block_info_list==null) read_key_block_info();
if(infoI_cache_.blockID==blockId)
return infoI_cache_;
if(infoI_cache==null)
infoI_cache = new cached_key_block();
try {
if(infoI==null)
infoI = _key_block_info_list[blockId];
infoI_cache.keys = new byte[(int) infoI.num_entries][];
infoI_cache.key_offsets = new long[(int) infoI.num_entries];
infoI_cache.hearderText = infoI.headerKeyText;
infoI_cache.tailerKeyText = infoI.tailerKeyText;
long start = infoI.key_block_compressed_size_accumulator;
long compressedSize;
if(blockId==_key_block_info_list.length-1)
compressedSize = _key_block_size - _key_block_info_list[_key_block_info_list.length-1].key_block_compressed_size_accumulator;
else
compressedSize = _key_block_info_list[blockId+1].key_block_compressed_size_accumulator-infoI.key_block_compressed_size_accumulator;
DataInputStream data_in = getStreamAt(_key_block_offset+start);
byte[] _key_block_compressed = new byte[(int) compressedSize];
data_in.read(_key_block_compressed, 0, _key_block_compressed.length);
data_in.close();
//int adler32 = getInt(_key_block_compressed[+4],_key_block_compressed[+5],_key_block_compressed[+6],_key_block_compressed[+7]);
byte[] key_block;
int BlockOff=0;
int BlockLen=(int) infoI.key_block_decompressed_size;
//解压开始
switch (_key_block_compressed[0]|_key_block_compressed[1]<<8|_key_block_compressed[2]<<16|_key_block_compressed[3]<<32){
default:
case 0://no compression
key_block=_key_block_compressed;
BlockOff=8;
break;
case 1:
key_block = new byte[BlockLen];
new LzoDecompressor1x().decompress(_key_block_compressed, 8, (int)(compressedSize-8), key_block, 0,new lzo_uintp());
break;
case 2:
key_block = new byte[BlockLen];
//key_block = zlib_decompress(_key_block_compressed,(int) (start+8),(int)(compressedSize-8));
Inflater inf = new Inflater();
inf.setInput(_key_block_compressed, +8,(int)(compressedSize-8));
try {
int ret = inf.inflate(key_block,0,(int)(infoI.key_block_decompressed_size));
} catch (DataFormatException e) {e.printStackTrace();}
break;
}
/*spliting current Key block*/
int key_start_index=0,
key_end_index,
keyCounter = 0;
while(key_start_index < BlockLen){
long key_id = _version<2 ?BU.toInt(key_block, BlockOff+key_start_index)
:BU.toLong(key_block, BlockOff+key_start_index);
key_end_index = key_start_index + _number_width;
SK_DELI:
while(key_end_index+delimiter_width<BlockLen){
for(int sker=0;sker<delimiter_width;sker++) {
if(key_block[BlockOff+key_end_index+sker]!=0) {
key_end_index+=delimiter_width;
continue SK_DELI;
}
}
break;//all match
}
//show("key_start_index"+key_start_index);
byte[] arraytmp = new byte[key_end_index-(key_start_index+_number_width)];
System.arraycopy(key_block,BlockOff+key_start_index+_number_width, arraytmp, 0,arraytmp.length);
//CMN.show(keyCounter+":::"+key_text);
key_start_index = key_end_index + delimiter_width;
//SU.Log(infoI_cache.keys.length+"~~~"+keyCounter+"~~~"+infoI.num_entries);
infoI_cache.keys[keyCounter]=arraytmp;
infoI_cache.key_offsets[keyCounter]=key_id;
keyCounter++;
}
//long end2=System.currentTimeMillis(); //获取开始时间
//System.out.println("解压耗时:"+(end2-start2));
//assert(adler32 == (calcChecksum(key_block)));
infoI_cache.blockID = blockId;
this.infoI_cache_=infoI_cache;
} catch (IOException e2) {
e2.printStackTrace();
}
return infoI_cache;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<>();
try {
while (!inflater.needsInput()) {
byte[] bytesDecompressedBuffer = new byte[numberOfBytesToDecompress];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = bytesDecompressedSoFar.get(b);
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}
/**
* zlib decompress 2 byte
*
* @param bytesToDecompress
* @return
*/
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
byte[] returnValues = null;
Inflater inflater = new Inflater();
int numberOfBytesToDecompress = bytesToDecompress.length;
inflater.setInput
(
bytesToDecompress,
0,
numberOfBytesToDecompress
);
int bufferSizeInBytes = numberOfBytesToDecompress;
int numberOfBytesDecompressedSoFar = 0;
List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();
try {
while (inflater.needsInput() == false) {
byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];
int numberOfBytesDecompressedThisTime = inflater.inflate
(
bytesDecompressedBuffer
);
numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;
for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
}
}
returnValues = new byte[bytesDecompressedSoFar.size()];
for (int b = 0; b < returnValues.length; b++) {
returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
}
} catch (DataFormatException dfe) {
dfe.printStackTrace();
}
inflater.end();
return returnValues;
}