Search in sources :

Example 41 with DataFormatException

use of java.util.zip.DataFormatException in project scheduling by ow2-proactive.

the class ObjectByteConverter method byteArrayToObject.

/**
 * Convert the given byte array into the corresponding object.
 * <p>
 * The given byteArray can be uncompressed if it has been compressed before.
 *
 * @param input the byteArray to be convert as an object.
 * @param uncompress true if the given byteArray must be also uncompressed, false if no compression was made on it.
 * @return the object corresponding to the given byteArray.
 * @throws IOException if an I/O exception occurs when writing the returned object
 * @throws ClassNotFoundException if class represented by given byteArray is not found.
 */
public static Object byteArrayToObject(byte[] input, boolean uncompress) throws IOException, ClassNotFoundException {
    if (input == null) {
        return null;
    }
    if (uncompress) {
        // Uncompress the bytes
        Inflater decompressor = new Inflater();
        decompressor.setInput(input);
        ByteArrayOutputStream bos = null;
        try {
            // Create an expandable byte array to hold the compressed data.
            bos = new ByteArrayOutputStream();
            // Compress the data
            byte[] buf = new byte[512];
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
            decompressor.end();
            // set the UNCOMPRESSED data
            input = bos.toByteArray();
        } catch (DataFormatException dfe) {
            // convert into io exception to fit previous behavior
            throw new IOException("Compressed data format is invalid : " + dfe.getMessage(), dfe);
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }
    // here, input byteArray is uncompressed if needed
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(input);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with DataFormatException

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.");
        }
        inflater.end();
    } else {
        throw new RuntimeException("PBF blob uses unsupported compression, only raw or zlib may be used.");
    }
    return blobData;
}
Also used : DataFormatException(java.util.zip.DataFormatException) Fileformat(org.openstreetmap.osmosis.osmbinary.Fileformat) Inflater(java.util.zip.Inflater)

Example 43 with DataFormatException

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);
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) ZipException(java.util.zip.ZipException) ByteBuffer(java.nio.ByteBuffer)

Example 44 with DataFormatException

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);
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) InvalidFrameException(org.jaudiotagger.tag.InvalidFrameException)

Example 45 with DataFormatException

use of java.util.zip.DataFormatException in project cassandra by apache.

the class DeflateCompressor method uncompressBuffer.

public void uncompressBuffer(ByteBuffer input, ByteBuffer output) throws IOException {
    try {
        Inflater inf = inflater.get();
        inf.reset();
        byte[] buffer = getThreadLocalScratchBuffer();
        // Use half the buffer for input, half for output.
        int chunkLen = buffer.length / 2;
        while (input.remaining() > chunkLen) {
            input.get(buffer, 0, chunkLen);
            inf.setInput(buffer, 0, chunkLen);
            while (!inf.needsInput()) {
                int len = inf.inflate(buffer, chunkLen, chunkLen);
                output.put(buffer, chunkLen, len);
            }
        }
        int inputLength = input.remaining();
        input.get(buffer, 0, inputLength);
        inf.setInput(buffer, 0, inputLength);
        while (!inf.needsInput()) {
            int len = inf.inflate(buffer, chunkLen, chunkLen);
            output.put(buffer, chunkLen, len);
        }
    } catch (DataFormatException e) {
        throw new IOException(e);
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Aggregations

DataFormatException (java.util.zip.DataFormatException)71 IOException (java.io.IOException)32 Inflater (java.util.zip.Inflater)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ByteBuffer (java.nio.ByteBuffer)9 ArrayList (java.util.ArrayList)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)5 File (java.io.File)4 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 ImageException (cbit.image.ImageException)2 ImageDataset (cbit.vcell.VirtualMicroscopy.ImageDataset)2 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 BaseMediaHeader (cbit.vcell.export.gloworm.atoms.BaseMediaHeader)2 BaseMediaInformation (cbit.vcell.export.gloworm.atoms.BaseMediaInformation)2 HandlerReference (cbit.vcell.export.gloworm.atoms.HandlerReference)2 VideoMediaInformation (cbit.vcell.export.gloworm.atoms.VideoMediaInformation)2 GeometryException (cbit.vcell.geometry.GeometryException)2