Search in sources :

Example 11 with DataFormatException

use of java.util.zip.DataFormatException in project commoncrawl-examples by commoncrawl.

the class GzipCompressorInputStream method read.

/**
     * {@inheritDoc}
     *
     * @since 1.1
     */
@Override
public int read(byte[] b, int off, int len) throws IOException {
    if (stoppedForEndOfMember || endOfStream) {
        return -1;
    }
    int size = 0;
    while (len > 0) {
        if (inf.needsInput()) {
            // Remember the current position because we may need to
            // rewind after reading too much input.
            in.mark(buf.length);
            bufUsed = in.read(buf);
            if (bufUsed == -1) {
                throw new EOFException();
            }
            inf.setInput(buf, 0, bufUsed);
        }
        int ret;
        try {
            ret = inf.inflate(b, off, len);
        } catch (DataFormatException e) {
            throw new IOException("Gzip-compressed data is corrupt");
        }
        crc.update(b, off, ret);
        memberSize += ret;
        off += ret;
        len -= ret;
        size += ret;
        count(ret);
        if (inf.finished()) {
            // We may have read too many bytes. Rewind the read
            // position to match the actual amount used.
            //
            // NOTE: The "if" is there just in case. Since we used
            // in.mark earler, it should always skip enough.
            in.reset();
            int skipAmount = bufUsed - inf.getRemaining();
            if (in.skip(skipAmount) != skipAmount) {
                throw new IOException();
            }
            bufUsed = 0;
            DataInputStream inData = new DataInputStream(in);
            // CRC32
            long crcStored = 0;
            for (int i = 0; i < 4; ++i) {
                crcStored |= (long) inData.readUnsignedByte() << (i * 8);
            }
            if (crcStored != crc.getValue()) {
                throw new IOException("Gzip-compressed data is corrupt " + "(CRC32 error)");
            }
            // Uncompressed size modulo 2^32 (ISIZE in the spec)
            int isize = 0;
            for (int i = 0; i < 4; ++i) {
                isize |= inData.readUnsignedByte() << (i * 8);
            }
            if (isize != memberSize) {
                throw new IOException("Gzip-compressed data is corrupt" + "(uncompressed size mismatch)");
            }
            if (!decompressConcatenated) {
                stoppedForEndOfMember = true;
            }
            // See if this is the end of the file.
            endOfStream = !init(false);
            if (stoppedForEndOfMember || endOfStream) {
                return size == 0 ? -1 : size;
            }
        }
    }
    return size;
}
Also used : DataFormatException(java.util.zip.DataFormatException) EOFException(java.io.EOFException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 12 with DataFormatException

use of java.util.zip.DataFormatException in project AndroidAsync by koush.

the class HeaderReader method readHeader.

public List<Header> readHeader(ByteBufferList bb, int length) throws IOException {
    byte[] bytes = new byte[length];
    bb.get(bytes);
    inflater.setInput(bytes);
    ByteBufferList source = new ByteBufferList().order(ByteOrder.BIG_ENDIAN);
    while (!inflater.needsInput()) {
        ByteBuffer b = ByteBufferList.obtain(8192);
        try {
            int read = inflater.inflate(b.array());
            b.limit(read);
            source.add(b);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
    }
    int numberOfPairs = source.getInt();
    List<Header> entries = new ArrayList<Header>(numberOfPairs);
    for (int i = 0; i < numberOfPairs; i++) {
        ByteString name = readByteString(source).toAsciiLowercase();
        ByteString values = readByteString(source);
        if (name.size() == 0)
            throw new IOException("name.size == 0");
        entries.add(new Header(name, values));
    }
    return entries;
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteBufferList(com.koushikdutta.async.ByteBufferList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 13 with DataFormatException

use of java.util.zip.DataFormatException in project phonegap-facebook-plugin by Wizcorp.

the class SpdyReader method readNameValueBlock.

private List<String> readNameValueBlock(int length) throws IOException {
    this.compressedLimit += length;
    try {
        int numberOfPairs = nameValueBlockIn.readInt();
        if (numberOfPairs < 0) {
            Logger.getLogger(getClass().getName()).warning("numberOfPairs < 0: " + numberOfPairs);
            throw ioException("numberOfPairs < 0");
        }
        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 ioException("name.length == 0");
            if (values.length() == 0)
                throw ioException("values.length == 0");
            entries.add(name);
            entries.add(values);
        }
        if (compressedLimit != 0) {
            Logger.getLogger(getClass().getName()).warning("compressedLimit > 0: " + compressedLimit);
        }
        return entries;
    } catch (DataFormatException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 14 with DataFormatException

use of java.util.zip.DataFormatException in project netty by netty.

the class JdkZlibDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (finished) {
        // Skip data received after finished.
        in.skipBytes(in.readableBytes());
        return;
    }
    int readableBytes = in.readableBytes();
    if (readableBytes == 0) {
        return;
    }
    if (decideZlibOrNone) {
        // First two bytes are needed to decide if it's a ZLIB stream.
        if (readableBytes < 2) {
            return;
        }
        boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
        inflater = new Inflater(nowrap);
        decideZlibOrNone = false;
    }
    if (crc != null) {
        switch(gzipState) {
            case FOOTER_START:
                if (readGZIPFooter(in)) {
                    finished = true;
                }
                return;
            default:
                if (gzipState != GzipState.HEADER_END) {
                    if (!readGZIPHeader(in)) {
                        return;
                    }
                }
        }
        // Some bytes may have been consumed, and so we must re-set the number of readable bytes.
        readableBytes = in.readableBytes();
    }
    if (in.hasArray()) {
        inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
    } else {
        byte[] array = new byte[readableBytes];
        in.getBytes(in.readerIndex(), array);
        inflater.setInput(array);
    }
    int maxOutputLength = inflater.getRemaining() << 1;
    ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);
    try {
        boolean readFooter = false;
        byte[] outArray = decompressed.array();
        while (!inflater.needsInput()) {
            int writerIndex = decompressed.writerIndex();
            int outIndex = decompressed.arrayOffset() + writerIndex;
            int length = decompressed.writableBytes();
            if (length == 0) {
                // completely filled the buffer allocate a new one and start to fill it
                out.add(decompressed);
                decompressed = ctx.alloc().heapBuffer(maxOutputLength);
                outArray = decompressed.array();
                continue;
            }
            int outputLength = inflater.inflate(outArray, outIndex, length);
            if (outputLength > 0) {
                decompressed.writerIndex(writerIndex + outputLength);
                if (crc != null) {
                    crc.update(outArray, outIndex, outputLength);
                }
            } else {
                if (inflater.needsDictionary()) {
                    if (dictionary == null) {
                        throw new DecompressionException("decompression failure, unable to set dictionary as non was specified");
                    }
                    inflater.setDictionary(dictionary);
                }
            }
            if (inflater.finished()) {
                if (crc == null) {
                    // Do not decode anymore.
                    finished = true;
                } else {
                    readFooter = true;
                }
                break;
            }
        }
        in.skipBytes(readableBytes - inflater.getRemaining());
        if (readFooter) {
            gzipState = GzipState.FOOTER_START;
            if (readGZIPFooter(in)) {
                finished = true;
            }
        }
    } catch (DataFormatException e) {
        throw new DecompressionException("decompression failure", e);
    } finally {
        if (decompressed.isReadable()) {
            out.add(decompressed);
        } else {
            decompressed.release();
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with DataFormatException

use of java.util.zip.DataFormatException in project voltdb by VoltDB.

the class HistogramLogReader method nextIntervalHistogram.

private EncodableHistogram nextIntervalHistogram(final double rangeStartTimeSec, final double rangeEndTimeSec, boolean absolute) {
    while (scanner.hasNextLine()) {
        try {
            if (scanner.hasNext("\\#.*")) {
                // Look for explicit start time or base time notes in comments:
                if (scanner.hasNext("#\\[StartTime:")) {
                    scanner.next("#\\[StartTime:");
                    if (scanner.hasNextDouble()) {
                        // start time represented as seconds since epoch
                        startTimeSec = scanner.nextDouble();
                        observedStartTime = true;
                    }
                } else if (scanner.hasNext("#\\[BaseTime:")) {
                    scanner.next("#\\[BaseTime:");
                    if (scanner.hasNextDouble()) {
                        // base time represented as seconds since epoch
                        baseTimeSec = scanner.nextDouble();
                        observedBaseTime = true;
                    }
                }
                scanner.nextLine();
                continue;
            }
            if (scanner.hasNext("\"StartTimestamp\".*")) {
                // Legend line
                scanner.nextLine();
                continue;
            }
            // Decode: startTimestamp, intervalLength, maxTime, histogramPayload
            // Timestamp is expected to be in seconds
            final double logTimeStampInSec = scanner.nextDouble();
            if (!observedStartTime) {
                // No explicit start time noted. Use 1st observed time:
                startTimeSec = logTimeStampInSec;
                observedStartTime = true;
            }
            if (!observedBaseTime) {
                // No explicit base time noted. Deduce from 1st observed time (compared to start time):
                if (logTimeStampInSec < startTimeSec - (365 * 24 * 3600.0)) {
                    // Criteria Note: if log timestamp is more than a year in the past (compared to
                    // StartTime), we assume that timestamps in the log are not absolute
                    baseTimeSec = startTimeSec;
                } else {
                    // Timestamps are absolute
                    baseTimeSec = 0.0;
                }
                observedBaseTime = true;
            }
            final double absoluteStartTimeStampSec = logTimeStampInSec + baseTimeSec;
            final double offsetStartTimeStampSec = absoluteStartTimeStampSec - startTimeSec;
            // Timestamp length is expect to be in seconds
            final double intervalLengthSec = scanner.nextDouble();
            final double absoluteEndTimeStampSec = absoluteStartTimeStampSec + intervalLengthSec;
            final double startTimeStampToCheckRangeOn = absolute ? absoluteStartTimeStampSec : offsetStartTimeStampSec;
            if (startTimeStampToCheckRangeOn < rangeStartTimeSec) {
                scanner.nextLine();
                continue;
            }
            if (startTimeStampToCheckRangeOn > rangeEndTimeSec) {
                return null;
            }
            // Skip maxTime field, as max time can be deduced from the histogram.
            scanner.nextDouble();
            final String compressedPayloadString = scanner.next();
            final ByteBuffer buffer = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(compressedPayloadString));
            EncodableHistogram histogram = EncodableHistogram.decodeFromCompressedByteBuffer(buffer, 0);
            histogram.setStartTimeStamp((long) (absoluteStartTimeStampSec * 1000.0));
            histogram.setEndTimeStamp((long) (absoluteEndTimeStampSec * 1000.0));
            // Move to next line. Very much needed for e.g. windows CR/LF lines
            scanner.nextLine();
            return histogram;
        } catch (java.util.NoSuchElementException ex) {
            return null;
        } catch (DataFormatException ex) {
            return null;
        }
    }
    return null;
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteBuffer(java.nio.ByteBuffer)

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