下面列出了java.awt.image.PixelInterleavedSampleModel#java.util.zip.DataFormatException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
try {
do {
int read = inflater.inflate(buffer, offset, length);
if(read <= 0) {
if(inflater.finished()) {
throw new EOFException();
}
if(inflater.needsInput()) {
refillInflater(inflater);
} else {
throw new IOException("Can't inflate " + length + " bytes");
}
} else {
offset += read;
length -= read;
}
} while(length > 0);
} catch (DataFormatException ex) {
throw (IOException)(new IOException("inflate error").initCause(ex));
}
}
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws DataFormatException {
if (in.readableBytes() != 0) {
int packetLength = PacketBuffer.readVarIntFromBuffer(in);
if (packetLength == 0) {
out.add(in.readBytes(in.readableBytes()));
} else {
if (packetLength < this.threshold) {
throw new DecoderException("Badly compressed packet - size of " + packetLength + " is below server threshold of " + this.threshold);
}
if (packetLength > 2097152) {
throw new DecoderException("Badly compressed packet - size of " + packetLength + " is larger than protocol maximum of " + 2097152);
}
byte[] compressedData = new byte[in.readableBytes()];
in.readBytes(compressedData);
this.inflater.setInput(compressedData);
byte[] decompressedData = new byte[packetLength];
this.inflater.inflate(decompressedData);
out.add(Unpooled.wrappedBuffer(decompressedData));
this.inflater.reset();
}
}
}
@Test
public void headerFExtraFlagWithMissingExtraLenFails() throws Exception {
gzipHeader[GZIP_HEADER_FLAG_INDEX] = (byte) (gzipHeader[GZIP_HEADER_FLAG_INDEX] | FEXTRA);
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(gzipHeader));
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(deflatedBytes));
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(gzipTrailer));
try {
byte[] b = new byte[originalData.length];
gzipInflatingBuffer.inflateBytes(b, 0, originalData.length);
fail("Expected DataFormatException");
} catch (DataFormatException expectedException) {
assertTrue(
"wrong exception message",
expectedException.getMessage().startsWith("Inflater data format exception:"));
}
}
@Test
public void headerFNameFlagWithMissingBytesFail() throws Exception {
gzipHeader[GZIP_HEADER_FLAG_INDEX] = (byte) (gzipHeader[GZIP_HEADER_FLAG_INDEX] | FNAME);
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(gzipHeader));
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(deflatedBytes));
gzipInflatingBuffer.addGzippedBytes(ReadableBuffers.wrap(gzipTrailer));
try {
byte[] b = new byte[originalData.length];
gzipInflatingBuffer.inflateBytes(b, 0, originalData.length);
fail("Expected DataFormatException");
} catch (DataFormatException expectedException) {
assertTrue(
"wrong exception message",
expectedException.getMessage().startsWith("Inflater data format exception:"));
}
}
/**
* Decompresses data. Throws runtime exception for malformed input.
* @param input Data to be decompressed.
* @return Decompressed data
*/
public static byte[] decompress(byte[] input) {
if (input == null) {
return null;
}
Decompresser decompresser = getDecompresser();
byte[] output;
try {
output = decompresser.decompress(input);
} catch (DataFormatException e) {
throw new RuntimeException("Unable to decompress data", e);
}
decompresser.release();
return output;
}
@Override
final int decompress(byte[] source, int sourceOffset, int sourceLength, byte[] target, int targetOffset, int rawDataLength) {
decompressor.setInput(source, sourceOffset, sourceLength );
int size;
try {
size = decompressor.inflate(target, targetOffset, rawDataLength);
if (!decompressor.finished()){
size= -1; // problem
}
} catch (DataFormatException e) {
size= -1;
}finally{
decompressor.reset();
}
return size;
}
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws DataFormatException {
if (in.readableBytes() != 0) {
int packetLength = PacketBuffer.readVarIntFromBuffer(in);
if (packetLength == 0) {
out.add(in.readBytes(in.readableBytes()));
} else {
if (packetLength < this.threshold) {
throw new DecoderException("Badly compressed packet - size of " + packetLength + " is below server threshold of " + this.threshold);
}
if (packetLength > 2097152) {
throw new DecoderException("Badly compressed packet - size of " + packetLength + " is larger than protocol maximum of " + 2097152);
}
byte[] compressedData = new byte[in.readableBytes()];
in.readBytes(compressedData);
this.inflater.setInput(compressedData);
byte[] decompressedData = new byte[packetLength];
this.inflater.inflate(decompressedData);
out.add(Unpooled.wrappedBuffer(decompressedData));
this.inflater.reset();
}
}
}
public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength) throws DataFormatException {
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] buffer = new byte[512];
byte[] result = new byte[0];
int bytesRead;
while (!decompresser.needsInput()) {
bytesRead = decompresser.inflate(buffer);
byte[] newResult = new byte[result.length + bytesRead];
System.arraycopy(result, 0, newResult, 0, result.length);
System.arraycopy(buffer, 0, newResult, result.length, bytesRead);
result = newResult;
}
// System.out.println(new String(result));
decompresser.end();
return new DeflaterInflaterData(result.length, result);
}
private static byte[] uncompressBytesInflateDeflate(byte[] inBytes) throws IOException {
Inflater inflater = new Inflater();
inflater.setInput(inBytes);
NonSyncByteArrayOutputStream bos = new NonSyncByteArrayOutputStream(inBytes.length);
byte[] buffer = new byte[1024 * 8];
while (!inflater.finished()) {
int count;
try {
count = inflater.inflate(buffer);
} catch (DataFormatException e) {
throw new IOException(e);
}
bos.write(buffer, 0, count);
}
byte[] output = bos.toByteArray();
return output;
}
public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
long startTime = System.currentTimeMillis();
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
logger.debug("Original: " + data.length + " bytes. " + "Decompressed: " + output.length + " bytes. Time: " + (System.currentTimeMillis() - startTime));
return output;
}
public static byte[] decompress(byte[] input) throws DataFormatException, IOException {
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(input);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
bos.close();
// Get the decompressed data
return bos.toByteArray();
}
private void bigTest(int step, int expectedAdler)
throws UnsupportedEncodingException, DataFormatException {
byte[] input = new byte[128 * 1024];
byte[] comp = new byte[128 * 1024 + 512];
byte[] output = new byte[128 * 1024 + 512];
Inflater inflater = new Inflater(false);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);
createSample(input, step);
compress(deflater, input, comp);
expand(inflater, comp, (int) deflater.getBytesWritten(), output);
assertEquals(inflater.getBytesWritten(), input.length);
assertEquals(deflater.getAdler(), inflater.getAdler());
assertEquals(deflater.getAdler(), expectedAdler);
}
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary parameters,
int index) throws IOException
{
final COSDictionary decodeParams = getDecodeParams(parameters, index);
try
{
decompress(encoded, Predictor.wrapPredictor(decoded, decodeParams));
}
catch (DataFormatException e)
{
// if the stream is corrupt a DataFormatException may occur
LOG.error("FlateFilter: stop reading corrupt stream due to a DataFormatException");
// re-throw the exception
throw new IOException(e);
}
return new DecodeResult(parameters);
}
/**
* Decompress the data bytes in the given message (in place).
*/
private void _decompressMessageData(Message msg) {
if((msg.flags & Message.FLAGS_COMPRESSED) == 0) {
throw new IllegalArgumentException("message data is not compressed");
}
Inflater decompresser = new Inflater();
decompresser.setInput(msg.data);
ByteArrayOutputStream bos = new ByteArrayOutputStream(msg.data.length);
byte[] buffer = new byte[8192];
try {
while (!decompresser.finished()) {
int size = decompresser.inflate(buffer);
bos.write(buffer, 0, size);
}
msg.data = bos.toByteArray();
msg.flags &= ~Message.FLAGS_COMPRESSED;
decompresser.end();
} catch (DataFormatException e) {
throw new PyroException("invalid compressed data: ", e);
}
}
@Override
public void decompress(byte[] bytes, int offset, int size, OutputStream out) throws IOException {
ZipInflater inflater = new ZipInflater();
inflater.reset();
inflater.setInput(bytes, offset, size);
int len;
byte[] block = new byte[1024];
try {
while (!inflater.finished()) {
len = inflater.inflate(block);
out.write(block, 0, len);
}
} catch (DataFormatException e) {
throw new IOException(e);
} finally {
inflater.end();
}
}
/**
* Reads byte array from the input stream. This method is not thread safe.
*
* @param buffer The buffer to hold the decompressed data. This buffer
* must be large enough to hold the decompressed data.
* @param decompressor java.util.Inflater
* @param input The data input stream.
* @return Returns the actual byte array (not compressed) read from the
* input stream.
* @throws IOException Thrown if unable to read from the input stream or
* unable to decompress the data.
*/
public static byte[] readByteArray(byte buffer[], Inflater decompressor, DataInput input) throws IOException
{
byte compressedBuffer[] = DataSerializer.readByteArray(input);
// Decompress the bytes
decompressor.setInput(compressedBuffer, 0, compressedBuffer.length);
byte retval[] = null;
try {
int resultLength = decompressor.inflate(buffer);
retval = new byte[resultLength];
System.arraycopy(compressedBuffer, 0, retval, 0, resultLength);
} catch (DataFormatException e) {
throw new IOException("Unable to decompress the byte array due to a data format error. " + e.getMessage());
}
return retval;
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public static byte[] uncompressZlib(byte[] bytes) throws IOException {
Inflater inflater = new Inflater();
inflater.setInput(bytes);
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
byte[] buf = new byte[1024];
while (!inflater.finished()) {
try {
int count = inflater.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e) {
}
}
bos.close();
inflater.end();
return bos.toByteArray();
}
protected void bufferAndDecompress() throws IOException {
if (allDataRead) {
eos = true;
return;
}
readChunkHeader();
fillBuffer(compressedBuffer);
inflater.setInput(compressedBuffer);
try {
inflater.inflate(buffer);
} catch (final DataFormatException e) {
throw new IOException(e);
}
inflater.reset();
bufferIndex = 0;
final int moreDataByte = in.read();
if (moreDataByte < 1) {
allDataRead = true;
} else if (moreDataByte > 1) {
throw new IOException("Expected indicator of whether or not more data was to come (-1, 0, or 1) but got " + moreDataByte);
}
}
public static TransferObject byteArrayToTransferObject(byte[] byteArray) throws DataFormatException, SocketExecuteException{
//TransferObject to = new TransferObject();
int receiveLength = byteArray.length;
byte[] toByteArray = new byte[receiveLength - 1];
System.arraycopy(byteArray, 1, toByteArray, 0, receiveLength - 1);
byte flagbyte = byteArray[0];
TransferObject to = new OrderedTransferObject();
if (TransferUtil.isCompress(flagbyte)) {
toByteArray = TransferUtil.getInputByCompress(toByteArray);
} else {
toByteArray = TransferUtil.getInputByNormal(toByteArray);
}
to.setByteData(toByteArray);
return to;
}
@Test
public void testGenerateAndRegenerateVirtualNetworkAndTravelData() throws IOException, ClassNotFoundException, DataFormatException {
File workingDirectory = MultiFileTools.getDefaultWorkingDirectory();
List<Long> virtualNetworkIds = new LinkedList<>();
for (int i = 0; i < 2; i++) {
Controler controler = prepare();
AmodeusModeConfig modeConfig = AmodeusConfigGroup.get(controler.getConfig()).getMode(AmodeusModeConfig.DEFAULT_MODE);
modeConfig.getDispatcherConfig().setRegenerateVirtualNetwork(true);
modeConfig.getDispatcherConfig().setRegenerateTravelData(true);
controler.run();
Assert.assertTrue(new File(workingDirectory, "generatedVirtualNetwork").exists());
Assert.assertTrue(new File(workingDirectory, "generatedTravelData").exists());
virtualNetworkIds.add(VirtualNetworkGet.readFile(controler.getScenario().getNetwork(), new File(workingDirectory, "generatedVirtualNetwork")).getvNetworkID());
}
// The network always gets regenerated, so we expect different IDs for the two runs.
Assert.assertNotEquals(virtualNetworkIds.get(0), virtualNetworkIds.get(1));
}
/**
* Decompress the zlib-compressed bytes and return an array of decompressed bytes
*
*/
public static byte[] decompress(byte compressedBytes[]) throws DataFormatException {
Inflater decompresser = new Inflater();
decompresser.setInput(compressedBytes);
byte[] resultBuffer = new byte[compressedBytes.length * 2];
byte[] resultTotal = new byte[0];
int resultLength = decompresser.inflate(resultBuffer);
while (resultLength > 0) {
byte previousResult[] = resultTotal;
resultTotal = new byte[resultTotal.length + resultLength];
System.arraycopy(previousResult, 0, resultTotal, 0, previousResult.length);
System.arraycopy(resultBuffer, 0, resultTotal, previousResult.length, resultLength);
resultLength = decompresser.inflate(resultBuffer);
}
decompresser.end();
return resultTotal;
}
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
try {
do {
int read = inflater.inflate(buffer, offset, length);
if(read <= 0) {
if(inflater.finished()) {
throw new EOFException();
}
if(inflater.needsInput()) {
refillInflater(inflater);
} else {
throw new IOException("Can't inflate " + length + " bytes");
}
} else {
offset += read;
length -= read;
}
} while(length > 0);
} catch (DataFormatException ex) {
throw (IOException)(new IOException("inflate error").initCause(ex));
}
}
/**
* Decompress the data bytes in the given message (in place).
*/
private void _decompressMessageData(Message msg) {
if((msg.flags & Message.FLAGS_COMPRESSED) == 0) {
throw new IllegalArgumentException("message data is not compressed");
}
Inflater decompresser = new Inflater();
decompresser.setInput(msg.data);
ByteArrayOutputStream bos = new ByteArrayOutputStream(msg.data.length);
byte[] buffer = new byte[8192];
try {
while (!decompresser.finished()) {
int size = decompresser.inflate(buffer);
bos.write(buffer, 0, size);
}
msg.data = bos.toByteArray();
msg.flags &= ~Message.FLAGS_COMPRESSED;
decompresser.end();
} catch (DataFormatException e) {
throw new PyroException("invalid compressed data: ", e);
}
}
@Override
public synchronized byte[] inflate(byte[] data, int maxSize) throws IOException {
INFLATER.reset();
INFLATER.setInput(data);
INFLATER.finished();
FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
bos.reset();
try {
int length = 0;
while (!INFLATER.finished()) {
int i = INFLATER.inflate(BUFFER);
length += i;
if (maxSize > 0 && length >= maxSize) {
throw new IOException("Inflated data exceeds maximum size");
}
bos.write(BUFFER, 0, i);
}
return bos.toByteArray();
} catch (DataFormatException e) {
throw new IOException("Unable to inflate zlib stream", e);
}
}
/**
* java.util.zip.Deflater#getBytesRead()
*/
public void test_getBytesWritten() throws DataFormatException, UnsupportedEncodingException {
// Regression test for HARMONY-158
Deflater def = new Deflater();
Inflater inf = new Inflater();
assertEquals(0, def.getTotalIn());
assertEquals(0, def.getTotalOut());
assertEquals(0, def.getBytesWritten());
// Encode a String into bytes
String inputString = "blahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
def.setInput(input);
def.finish();
def.deflate(output);
inf.setInput(output);
int compressedDataLength = inf.inflate(input);
assertEquals(16, inf.getTotalIn());
assertEquals(compressedDataLength, inf.getTotalOut());
assertEquals(14, inf.getBytesWritten());
def.end();
inf.end();
}
@Override
public void inflate(ByteBuf source, ByteBuf destination, int max) throws DataFormatException {
ensureNotDisposed();
source.memoryAddress();
destination.memoryAddress();
while (!inflate.finished && source.isReadable()) {
if (!destination.isWritable()) {
ensureMaxSize(destination, max);
destination.ensureWritable(ZLIB_BUFFER_SIZE);
}
int produced = inflate.process(inflateCtx, source.memoryAddress() + source.readerIndex(),
source.readableBytes(), destination.memoryAddress() + destination.writerIndex(),
destination.writableBytes());
source.readerIndex(source.readerIndex() + inflate.consumed);
destination.writerIndex(destination.writerIndex() + produced);
}
inflate.reset(inflateCtx);
inflate.consumed = 0;
inflate.finished = false;
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
/**
* Uncompress the supplied {@link IOBuffer}
*
* @param buffer
*/
private void uncompress(IOBuffer buffer) {
if((this.flags & FLAG_COMPRESSED) != 0) {
int numberOfBytesToSkip = 2; /* Skip the Protocol and Flags bytes */
this.inflater.setInput(buffer.array(), numberOfBytesToSkip, buffer.limit()-numberOfBytesToSkip);
int len = 0;
try {
len = this.inflater.inflate(this.compressionBuffer.array(), numberOfBytesToSkip, this.compressionBuffer.capacity()-numberOfBytesToSkip);
}
catch(DataFormatException e) {
throw new RuntimeException(e);
}
this.numberOfBytesCompressed = len-(buffer.limit()-numberOfBytesToSkip);
this.compressionBuffer.position(2);
this.compressionBuffer.limit(len);
buffer.limit(len+numberOfBytesToSkip);
buffer.position(numberOfBytesToSkip);
buffer.put(this.compressionBuffer);
buffer.position(numberOfBytesToSkip);
}
}
/**
* java.util.zip.Deflater#deflate(byte[])
*/
public void test_deflate$B() {
byte outPutBuf[] = new byte[50];
byte byteArray[] = { 1, 3, 4, 7, 8 };
byte outPutInf[] = new byte[50];
int x = 0;
Deflater defl = new Deflater();
defl.setInput(byteArray);
defl.finish();
while (!defl.finished()) {
x += defl.deflate(outPutBuf);
}
assertEquals("Deflater at end of stream, should return 0", 0, defl
.deflate(outPutBuf));
int totalOut = defl.getTotalOut();
int totalIn = defl.getTotalIn();
assertEquals(x, totalOut);
assertEquals(byteArray.length, totalIn);
defl.end();
Inflater infl = new Inflater();
try {
infl.setInput(outPutBuf);
while (!infl.finished()) {
infl.inflate(outPutInf);
}
} catch (DataFormatException e) {
fail("Invalid input to be decompressed");
}
assertEquals(totalIn, infl.getTotalOut());
assertEquals(totalOut, infl.getTotalIn());
for (int i = 0; i < byteArray.length; i++) {
assertEquals(byteArray[i], outPutInf[i]);
}
assertEquals("Final decompressed data contained more bytes than original",
0, outPutInf[byteArray.length]);
infl.end();
}