java.util.zip.DataFormatException Java Examples

The following examples show how to use java.util.zip.DataFormatException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Protocol.java    From seventh with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #2
Source File: ZipUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: GzipInflatingBufferTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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:"));
  }
}
 
Example #4
Source File: GzipInflatingBufferTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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:"));
  }
}
 
Example #5
Source File: NetworkCompressionDecoder.java    From The-5zig-Mod with MIT License 6 votes vote down vote up
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();
		}
	}
}
 
Example #6
Source File: CliUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
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);
  }
 
Example #7
Source File: NetworkCompressionDecoder.java    From The-5zig-Mod with GNU General Public License v3.0 6 votes vote down vote up
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();
		}
	}
}
 
Example #8
Source File: CloverDataStream.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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;
}
 
Example #9
Source File: TezUtilsInternal.java    From tez with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: CompressionUtils.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #11
Source File: Misc.java    From iaf with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #12
Source File: OldAndroidDeflateTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #13
Source File: FlateFilter.java    From sambox with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #14
Source File: PyroProxy.java    From Pyrolite with MIT License 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #15
Source File: ZlibCompressor.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example #16
Source File: PNGDecoder.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
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));
    }
}
 
Example #17
Source File: NameValueBlockReader.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
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());
  }
}
 
Example #18
Source File: Compression.java    From lumongo with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #19
Source File: CompressionInputStream.java    From nifi with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #20
Source File: TransferUtil.java    From Tatala-RPC with Apache License 2.0 6 votes vote down vote up
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;
  }
 
Example #21
Source File: MATSimVirtualNetworkTravelDataTest.java    From amodeus with GNU General Public License v2.0 6 votes vote down vote up
@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));
}
 
Example #22
Source File: CompressionUtils.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: PNGDecoder.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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));
    }
}
 
Example #24
Source File: PyroProxy.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #25
Source File: ZlibSingleThreadLowMem.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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);
    }
}
 
Example #26
Source File: InflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #27
Source File: NativeVelocityCompressor.java    From Velocity with MIT License 6 votes vote down vote up
@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;
}
 
Example #28
Source File: NameValueBlockReader.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
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());
  }
}
 
Example #29
Source File: DataSerializerEx.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #30
Source File: TransferUtil.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
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 = null;
//check if new version of transfer object
if(TransferUtil.isNewVersion(flagbyte)){
	to = new NewTransferObject();
} else {
	to = new StandardTransferObject();
}

if (TransferUtil.isCompress(flagbyte)) {
	toByteArray = TransferUtil.getInputByCompress(toByteArray);
} else {
	toByteArray = TransferUtil.getInputByNormal(toByteArray);
}
to.setByteData(toByteArray);

//set long connection flag
if(TransferUtil.isLongConnection(flagbyte)){
	to.setLongConnection(true);
}

return to;
  }