@Test(expected = ZipException.class) public void not_a_zip_file_3() throws Exception { zero_byte_compressed[0] = 0x01; zero_byte_compressed[1] = 0x01; final GzipDecodingBufferSupplier decoder = new GzipDecodingBufferSupplier(new ByteArrayBufferSupplier(zero_byte_compressed)); final ByteBuffer buf = ByteBuffer.allocate(32); decoder.load(buf); }
@Override public void load(ByteBuffer buf) throws IOException { for (;;) { if (end_) return; if (inflater_.finished()) { raw_buf_.position(raw_buf_.limit() - inflater_.getRemaining()); LOG.log(Level.FINE, "reading trailer..."); try { end_ = read_trailer_(); } catch (IOException ex) { LOG.log(Level.FINE, "error reading trailer, closing file as a precaution...", ex); end_ = true; throw ex; } if (end_) return; continue; } if (inflater_.needsInput()) { raw_buf_.clear(); raw_.load(raw_buf_); raw_buf_.flip(); inflater_.setInput(raw_buf_.array(), raw_buf_.arrayOffset() + raw_buf_.position(), raw_buf_.remaining()); } if (!buf.hasRemaining()) return; try { final int len; if (buf.hasArray()) { len = inflater_.inflate(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); crc_.update(buf.array(), buf.arrayOffset() + buf.position(), len); buf.position(buf.position() + len); } else { byte[] tmp = new byte[buf.remaining()]; len = inflater_.inflate(tmp); crc_.update(tmp, 0, len); buf.put(tmp, 0, len); } LOG.log(Level.FINE, "inflated {0} bytes (total so far {1} bytes)", new Object[]{len, inflater_.getBytesWritten()}); } catch (DataFormatException ex) { LOG.log(Level.WARNING, "unable to inflate", ex); throw new ZipException(Optional.ofNullable(ex.getMessage()).orElse("Invalid ZLIB data format")); } } }
GzipDecodingBufferSupplier implements BufferSupplier { @Override public void load(ByteBuffer buf) throws IOException { for (;;) { if (end_) return; if (inflater_.finished()) { raw_buf_.position(raw_buf_.limit() - inflater_.getRemaining()); LOG.log(Level.FINE, "reading trailer..."); try { end_ = read_trailer_(); } catch (IOException ex) { LOG.log(Level.FINE, "error reading trailer, closing file as a precaution...", ex); end_ = true; throw ex; } if (end_) return; continue; } if (inflater_.needsInput()) { raw_buf_.clear(); raw_.load(raw_buf_); raw_buf_.flip(); inflater_.setInput(raw_buf_.array(), raw_buf_.arrayOffset() + raw_buf_.position(), raw_buf_.remaining()); } if (!buf.hasRemaining()) return; try { final int len; if (buf.hasArray()) { len = inflater_.inflate(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); crc_.update(buf.array(), buf.arrayOffset() + buf.position(), len); buf.position(buf.position() + len); } else { byte[] tmp = new byte[buf.remaining()]; len = inflater_.inflate(tmp); crc_.update(tmp, 0, len); buf.put(tmp, 0, len); } LOG.log(Level.FINE, "inflated {0} bytes (total so far {1} bytes)", new Object[]{len, inflater_.getBytesWritten()}); } catch (DataFormatException ex) { LOG.log(Level.WARNING, "unable to inflate", ex); throw new ZipException(Optional.ofNullable(ex.getMessage()).orElse("Invalid ZLIB data format")); } } } }
GzipDecodingBufferSupplier implements BufferSupplier { @Override public void load(ByteBuffer buf) throws IOException { for (;;) { if (end_) return; if (inflater_.finished()) { raw_buf_.position(raw_buf_.limit() - inflater_.getRemaining()); LOG.log(Level.FINE, "reading trailer..."); try { end_ = read_trailer_(); } catch (IOException ex) { LOG.log(Level.FINE, "error reading trailer, closing file as a precaution...", ex); end_ = true; throw ex; } if (end_) return; continue; } if (inflater_.needsInput()) { raw_buf_.clear(); raw_.load(raw_buf_); raw_buf_.flip(); inflater_.setInput(raw_buf_.array(), raw_buf_.arrayOffset() + raw_buf_.position(), raw_buf_.remaining()); } if (!buf.hasRemaining()) return; try { final int len; if (buf.hasArray()) { len = inflater_.inflate(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); crc_.update(buf.array(), buf.arrayOffset() + buf.position(), len); buf.position(buf.position() + len); } else { byte[] tmp = new byte[buf.remaining()]; len = inflater_.inflate(tmp); crc_.update(tmp, 0, len); buf.put(tmp, 0, len); } LOG.log(Level.FINE, "inflated {0} bytes (total so far {1} bytes)", new Object[]{len, inflater_.getBytesWritten()}); } catch (DataFormatException ex) { LOG.log(Level.WARNING, "unable to inflate", ex); throw new ZipException(Optional.ofNullable(ex.getMessage()).orElse("Invalid ZLIB data format")); } } } GzipDecodingBufferSupplier(BufferSupplier raw); }
GzipDecodingBufferSupplier implements BufferSupplier { @Override public void load(ByteBuffer buf) throws IOException { for (;;) { if (end_) return; if (inflater_.finished()) { raw_buf_.position(raw_buf_.limit() - inflater_.getRemaining()); LOG.log(Level.FINE, "reading trailer..."); try { end_ = read_trailer_(); } catch (IOException ex) { LOG.log(Level.FINE, "error reading trailer, closing file as a precaution...", ex); end_ = true; throw ex; } if (end_) return; continue; } if (inflater_.needsInput()) { raw_buf_.clear(); raw_.load(raw_buf_); raw_buf_.flip(); inflater_.setInput(raw_buf_.array(), raw_buf_.arrayOffset() + raw_buf_.position(), raw_buf_.remaining()); } if (!buf.hasRemaining()) return; try { final int len; if (buf.hasArray()) { len = inflater_.inflate(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); crc_.update(buf.array(), buf.arrayOffset() + buf.position(), len); buf.position(buf.position() + len); } else { byte[] tmp = new byte[buf.remaining()]; len = inflater_.inflate(tmp); crc_.update(tmp, 0, len); buf.put(tmp, 0, len); } LOG.log(Level.FINE, "inflated {0} bytes (total so far {1} bytes)", new Object[]{len, inflater_.getBytesWritten()}); } catch (DataFormatException ex) { LOG.log(Level.WARNING, "unable to inflate", ex); throw new ZipException(Optional.ofNullable(ex.getMessage()).orElse("Invalid ZLIB data format")); } } } GzipDecodingBufferSupplier(BufferSupplier raw); @Override void load(ByteBuffer buf); @Override boolean atEof(); Optional<String> getFname(); Optional<String> getComment(); }
GzipDecodingBufferSupplier implements BufferSupplier { @Override public void load(ByteBuffer buf) throws IOException { for (;;) { if (end_) return; if (inflater_.finished()) { raw_buf_.position(raw_buf_.limit() - inflater_.getRemaining()); LOG.log(Level.FINE, "reading trailer..."); try { end_ = read_trailer_(); } catch (IOException ex) { LOG.log(Level.FINE, "error reading trailer, closing file as a precaution...", ex); end_ = true; throw ex; } if (end_) return; continue; } if (inflater_.needsInput()) { raw_buf_.clear(); raw_.load(raw_buf_); raw_buf_.flip(); inflater_.setInput(raw_buf_.array(), raw_buf_.arrayOffset() + raw_buf_.position(), raw_buf_.remaining()); } if (!buf.hasRemaining()) return; try { final int len; if (buf.hasArray()) { len = inflater_.inflate(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); crc_.update(buf.array(), buf.arrayOffset() + buf.position(), len); buf.position(buf.position() + len); } else { byte[] tmp = new byte[buf.remaining()]; len = inflater_.inflate(tmp); crc_.update(tmp, 0, len); buf.put(tmp, 0, len); } LOG.log(Level.FINE, "inflated {0} bytes (total so far {1} bytes)", new Object[]{len, inflater_.getBytesWritten()}); } catch (DataFormatException ex) { LOG.log(Level.WARNING, "unable to inflate", ex); throw new ZipException(Optional.ofNullable(ex.getMessage()).orElse("Invalid ZLIB data format")); } } } GzipDecodingBufferSupplier(BufferSupplier raw); @Override void load(ByteBuffer buf); @Override boolean atEof(); Optional<String> getFname(); Optional<String> getComment();  }