use of java.util.zip.DataFormatException in project cas by apereo.
the class CompressionUtils method inflate.
/**
* Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
*
* @param bytes the bytes
* @return the array as a string with {@code UTF-8} encoding
*/
public static String inflate(final byte[] bytes) {
final Inflater inflater = new Inflater(true);
final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH];
final byte[] extendedBytes = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
extendedBytes[bytes.length] = 0;
inflater.setInput(extendedBytes);
try {
final int resultLength = inflater.inflate(xmlMessageBytes);
inflater.end();
if (!inflater.finished()) {
throw new RuntimeException("buffer not large enough.");
}
inflater.end();
return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8);
} catch (final DataFormatException e) {
return null;
}
}
use of java.util.zip.DataFormatException in project jetty.project by eclipse.
the class GZIPContentDecoder method decodeChunks.
/**
* Inflate compressed data.
* <p>Inflation continues until the compressed block end is reached, there is no
* more compressed data or a call to {@link #decodedChunk(ByteBuffer)} returns true.
* @param compressed Buffer of compressed data to inflate
*/
protected void decodeChunks(ByteBuffer compressed) {
ByteBuffer buffer = null;
try {
while (true) {
switch(_state) {
case INITIAL:
{
_state = State.ID;
break;
}
case FLAGS:
{
if ((_flags & 0x04) == 0x04) {
_state = State.EXTRA_LENGTH;
_size = 0;
_value = 0;
} else if ((_flags & 0x08) == 0x08)
_state = State.NAME;
else if ((_flags & 0x10) == 0x10)
_state = State.COMMENT;
else if ((_flags & 0x2) == 0x2) {
_state = State.HCRC;
_size = 0;
_value = 0;
} else {
_state = State.DATA;
continue;
}
break;
}
case DATA:
{
while (true) {
if (buffer == null)
buffer = acquire(_bufferSize);
try {
int length = _inflater.inflate(buffer.array(), buffer.arrayOffset(), buffer.capacity());
buffer.limit(length);
} catch (DataFormatException x) {
throw new ZipException(x.getMessage());
}
if (buffer.hasRemaining()) {
ByteBuffer chunk = buffer;
buffer = null;
if (decodedChunk(chunk))
return;
} else if (_inflater.needsInput()) {
if (!compressed.hasRemaining())
return;
if (compressed.hasArray()) {
_inflater.setInput(compressed.array(), compressed.arrayOffset() + compressed.position(), compressed.remaining());
compressed.position(compressed.limit());
} else {
// TODO use the pool
byte[] input = new byte[compressed.remaining()];
compressed.get(input);
_inflater.setInput(input);
}
} else if (_inflater.finished()) {
int remaining = _inflater.getRemaining();
compressed.position(compressed.limit() - remaining);
_state = State.CRC;
_size = 0;
_value = 0;
break;
}
}
continue;
}
default:
break;
}
if (!compressed.hasRemaining())
break;
byte currByte = compressed.get();
switch(_state) {
case ID:
{
_value += (currByte & 0xFF) << 8 * _size;
++_size;
if (_size == 2) {
if (_value != 0x8B1F)
throw new ZipException("Invalid gzip bytes");
_state = State.CM;
}
break;
}
case CM:
{
if ((currByte & 0xFF) != 0x08)
throw new ZipException("Invalid gzip compression method");
_state = State.FLG;
break;
}
case FLG:
{
_flags = currByte;
_state = State.MTIME;
_size = 0;
_value = 0;
break;
}
case MTIME:
{
// Skip the 4 MTIME bytes
++_size;
if (_size == 4)
_state = State.XFL;
break;
}
case XFL:
{
// Skip XFL
_state = State.OS;
break;
}
case OS:
{
// Skip OS
_state = State.FLAGS;
break;
}
case EXTRA_LENGTH:
{
_value += (currByte & 0xFF) << 8 * _size;
++_size;
if (_size == 2)
_state = State.EXTRA;
break;
}
case EXTRA:
{
// Skip EXTRA bytes
--_value;
if (_value == 0) {
// Clear the EXTRA flag and loop on the flags
_flags &= ~0x04;
_state = State.FLAGS;
}
break;
}
case NAME:
{
// Skip NAME bytes
if (currByte == 0) {
// Clear the NAME flag and loop on the flags
_flags &= ~0x08;
_state = State.FLAGS;
}
break;
}
case COMMENT:
{
// Skip COMMENT bytes
if (currByte == 0) {
// Clear the COMMENT flag and loop on the flags
_flags &= ~0x10;
_state = State.FLAGS;
}
break;
}
case HCRC:
{
// Skip HCRC
++_size;
if (_size == 2) {
// Clear the HCRC flag and loop on the flags
_flags &= ~0x02;
_state = State.FLAGS;
}
break;
}
case CRC:
{
_value += (currByte & 0xFF) << 8 * _size;
++_size;
if (_size == 4) {
// From RFC 1952, compliant decoders need not to verify the CRC
_state = State.ISIZE;
_size = 0;
_value = 0;
}
break;
}
case ISIZE:
{
_value += (currByte & 0xFF) << 8 * _size;
++_size;
if (_size == 4) {
if (_value != _inflater.getBytesWritten())
throw new ZipException("Invalid input size");
// TODO ByteBuffer result = output == null ? BufferUtil.EMPTY_BUFFER : ByteBuffer.wrap(output);
reset();
return;
}
break;
}
default:
throw new ZipException();
}
}
} catch (ZipException x) {
throw new RuntimeException(x);
} finally {
if (buffer != null)
release(buffer);
}
}
use of java.util.zip.DataFormatException in project j2objc by google.
the class OldDataFormatExceptionTest method testDataFormatExceptionString.
public void testDataFormatExceptionString() {
DataFormatException dfe = new DataFormatException("Test");
assertEquals(dfe.getMessage(), "Test");
}
use of java.util.zip.DataFormatException in project graphhopper by graphhopper.
the class PbfBlobDecoder method readBlobContent.
private byte[] readBlobContent() throws IOException {
Fileformat.Blob blob = Fileformat.Blob.parseFrom(rawBlob);
byte[] blobData;
if (blob.hasRaw()) {
blobData = blob.getRaw().toByteArray();
} else if (blob.hasZlibData()) {
Inflater inflater = new Inflater();
inflater.setInput(blob.getZlibData().toByteArray());
blobData = new byte[blob.getRawSize()];
try {
inflater.inflate(blobData);
} catch (DataFormatException e) {
throw new RuntimeException("Unable to decompress PBF blob.", e);
}
if (!inflater.finished()) {
throw new RuntimeException("PBF blob contains incomplete compressed data.");
}
} else {
throw new RuntimeException("PBF blob uses unsupported compression, only raw or zlib may be used.");
}
return blobData;
}
use of java.util.zip.DataFormatException in project MusicDNA by harjot-oberai.
the class ID3Compression method uncompress.
/**
* Decompress realFrameSize bytes to decompressedFrameSize bytes and return as ByteBuffer
*
* @param byteBuffer
* @param decompressedFrameSize
* @param realFrameSize
* @return
* @throws org.jaudiotagger.tag.InvalidFrameException
*
*/
protected static ByteBuffer uncompress(String identifier, String filename, ByteBuffer byteBuffer, int decompressedFrameSize, int realFrameSize) throws InvalidFrameException {
logger.config(filename + ":About to decompress " + realFrameSize + " bytes, expect result to be:" + decompressedFrameSize + " bytes");
// Decompress the bytes into this buffer, size initialized from header field
byte[] result = new byte[decompressedFrameSize];
byte[] input = new byte[realFrameSize];
//Store position ( just after frame header and any extra bits)
//Read frame data into array, and then put buffer back to where it was
int position = byteBuffer.position();
byteBuffer.get(input, 0, realFrameSize);
byteBuffer.position(position);
Inflater decompresser = new Inflater();
decompresser.setInput(input);
try {
int inflatedTo = decompresser.inflate(result);
logger.config(filename + ":Decompressed to " + inflatedTo + " bytes");
} catch (DataFormatException dfe) {
logger.log(Level.CONFIG, "Unable to decompress this frame:" + identifier, dfe);
//Update position of main buffer, so no attempt is made to reread these bytes
byteBuffer.position(byteBuffer.position() + realFrameSize);
throw new InvalidFrameException(ErrorMessage.ID3_UNABLE_TO_DECOMPRESS_FRAME.getMsg(identifier, filename, dfe.getMessage()));
}
decompresser.end();
return ByteBuffer.wrap(result);
}
Aggregations