@Test public void inflateSingleByte() throws Exception { Buffer inflated = new Buffer(); Buffer deflated = decodeBase64( "eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tKtYDAF6CD5s="); InflaterSource source = new InflaterSource(deflated, new Inflater()); source.read(inflated, 1); source.close(); assertEquals("G", inflated.readUtf8()); assertEquals(0, inflated.size()); }
/** Returns a new buffer containing the inflated contents of {@code deflated}. */ private Buffer inflate(Buffer deflated) throws IOException { Buffer result = new Buffer(); InflaterSource source = new InflaterSource(deflated, new Inflater()); while (source.read(result, Integer.MAX_VALUE) != -1) { } return result; } }
public GzipSource(Source source) { if (source == null) throw new IllegalArgumentException("source == null"); this.inflater = new Inflater(true); this.source = Okio.buffer(source); this.inflaterSource = new InflaterSource(this.source, inflater); }
private void doneReading() throws IOException { // Move any outstanding unread bytes into the inflater. One side-effect of // deflate compression is that sometimes there are bytes remaining in the // stream after we've consumed all of the content. if (compressedLimit > 0) { inflaterSource.refill(); if (compressedLimit != 0) throw new IOException("compressedLimit > 0: " + compressedLimit); } }
boolean sourceExhausted = refill(); releaseInflatedBytes(); if (tail.pos == tail.limit) {
@Override public void close() throws IOException { inflaterSource.close(); }
long result = inflaterSource.read(sink, byteCount); if (result != -1) { updateCrc(sink, offset, result);
/** * Refills the inflater with compressed data if it needs input. (And only if * it needs input). Returns true if the inflater required input but the source * was exhausted. */ public boolean refill() throws IOException { if (!inflater.needsInput()) return false; releaseInflatedBytes(); if (inflater.getRemaining() != 0) throw new IllegalStateException("?"); // TODO: possible? // If there are compressed bytes in the source, assign them to the inflater. if (source.exhausted()) return true; // Assign buffer bytes to the inflater. Segment head = source.buffer().head; bufferBytesHeldByInflater = head.limit - head.pos; inflater.setInput(head.data, head.pos, bufferBytesHeldByInflater); return false; }
public NameValueBlockReader(BufferedSource source) { // Limit the inflater input stream to only those bytes in the Name/Value // block. We cut the inflater off at its source because we can't predict the // ratio of compressed bytes to uncompressed bytes. Source throttleSource = new ForwardingSource(source) { @Override public long read(Buffer sink, long byteCount) throws IOException { if (compressedLimit == 0) return -1; // Out of data for the current block. long read = super.read(sink, Math.min(byteCount, compressedLimit)); if (read == -1) return -1; compressedLimit -= read; return read; } }; // Subclass inflater to install a dictionary when it's needed. Inflater inflater = new Inflater() { @Override public int inflate(byte[] buffer, int offset, int count) throws DataFormatException { int result = super.inflate(buffer, offset, count); if (result == 0 && needsDictionary()) { setDictionary(Spdy3.DICTIONARY); result = super.inflate(buffer, offset, count); } return result; } }; this.inflaterSource = new InflaterSource(throttleSource, inflater); this.source = Okio.buffer(inflaterSource); }
@Test public void inflateIntoNonemptySink() throws Exception { for (int i = 0; i < SEGMENT_SIZE; i++) { Buffer inflated = new Buffer().writeUtf8(repeat('a', i)); Buffer deflated = decodeBase64( "eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tKtYDAF6CD5s="); InflaterSource source = new InflaterSource(deflated, new Inflater()); while (source.read(inflated, Integer.MAX_VALUE) != -1) { } inflated.skip(i); assertEquals("God help us, we're in the hands of engineers.", inflated.readUtf8()); } }
private void doneReading() throws IOException { // Move any outstanding unread bytes into the inflater. One side-effect of // deflate compression is that sometimes there are bytes remaining in the // stream after we've consumed all of the content. if (compressedLimit > 0) { inflaterSource.refill(); if (compressedLimit != 0) throw new IOException("compressedLimit > 0: " + compressedLimit); } }
@Test public void inflateByteCount() throws Exception { Buffer inflated = new Buffer(); Buffer deflated = decodeBase64( "eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tKtYDAF6CD5s="); InflaterSource source = new InflaterSource(deflated, new Inflater()); source.read(inflated, 11); source.close(); assertEquals("God help us", inflated.readUtf8()); assertEquals(0, inflated.size()); }
public NameValueBlockReader(BufferedSource source) { // Limit the inflater input stream to only those bytes in the Name/Value // block. We cut the inflater off at its source because we can't predict the // ratio of compressed bytes to uncompressed bytes. Source throttleSource = new ForwardingSource(source) { @Override public long read(Buffer sink, long byteCount) throws IOException { if (compressedLimit == 0) return -1; // Out of data for the current block. long read = super.read(sink, Math.min(byteCount, compressedLimit)); if (read == -1) return -1; compressedLimit -= read; return read; } }; // Subclass inflater to install a dictionary when it's needed. Inflater inflater = new Inflater() { @Override public int inflate(byte[] buffer, int offset, int count) throws DataFormatException { int result = super.inflate(buffer, offset, count); if (result == 0 && needsDictionary()) { setDictionary(Spdy3.DICTIONARY); result = super.inflate(buffer, offset, count); } return result; } }; this.inflaterSource = new InflaterSource(throttleSource, inflater); this.source = Okio.buffer(inflaterSource); }
private void doneReading() throws IOException { // Move any outstanding unread bytes into the inflater. One side-effect of // deflate compression is that sometimes there are bytes remaining in the // stream after we've consumed all of the content. if (compressedLimit > 0) { inflaterSource.refill(); if (compressedLimit != 0) throw new IOException("compressedLimit > 0: " + compressedLimit); } }
public NameValueBlockReader(BufferedSource source) { // Limit the inflater input stream to only those bytes in the Name/Value // block. We cut the inflater off at its source because we can't predict the // ratio of compressed bytes to uncompressed bytes. Source throttleSource = new ForwardingSource(source) { @Override public long read(Buffer sink, long byteCount) throws IOException { if (compressedLimit == 0) return -1; // Out of data for the current block. long read = super.read(sink, Math.min(byteCount, compressedLimit)); if (read == -1) return -1; compressedLimit -= read; return read; } }; // Subclass inflater to install a dictionary when it's needed. Inflater inflater = new Inflater() { @Override public int inflate(byte[] buffer, int offset, int count) throws DataFormatException { int result = super.inflate(buffer, offset, count); if (result == 0 && needsDictionary()) { setDictionary(Spdy3.DICTIONARY); result = super.inflate(buffer, offset, count); } return result; } }; this.inflaterSource = new InflaterSource(throttleSource, inflater); this.source = Okio.buffer(inflaterSource); }