public void addFile(String name, byte[] content, boolean deflate) throws IOException { byte[] data; if (deflate) { ByteArrayInputStream in = new ByteArrayInputStream(content); DeflaterInputStream def = new DeflaterInputStream(in, new Deflater(9, true)); data = ByteStreams.toByteArray(def); } else { data = content; } Chain dataChain = new Chain(data, (int) this.getHeader().getDefaultChunkSize(), getContainerContext()); dataChain.setContainerContext(getContainerContext()); Date date = new Date(); Attributes attributes = new Attributes(date, date, 0, name, null, getContainerContext()); Chain attrChain = new Chain(attributes.asByteArray(), (int) this.getHeader().getDefaultChunkSize(), getContainerContext()); attrChain.setContainerContext(getContainerContext()); attributes.setChain(attrChain); File fl = new File(name, attributes, dataChain, this, getContainerContext()); this.getChains().add(attrChain); this.getChains().add(dataChain); this.getFiles().put(fl.getName(), fl); }
public void testReadByteByByte() throws IOException { byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(1, in.available()); int b; while ((b = in.read()) != -1) { out.write(b); } assertEquals(0, in.available()); assertEquals(Arrays.toString(data), Arrays.toString(inflate(out.toByteArray()))); in.close(); try { in.available(); fail(); } catch (IOException expected) { } }
public void testReadWithBuffer() throws IOException { byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; byte[] buffer = new byte[8]; InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data)); ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(1, in.available()); int count; while ((count = in.read(buffer, 0, 5)) != -1) { assertTrue(count <= 5); out.write(buffer, 0, count); } assertEquals(0, in.available()); assertEquals(Arrays.toString(data), Arrays.toString(inflate(out.toByteArray()))); in.close(); try { in.available(); fail(); } catch (IOException expected) { } }
/** * Reads input for service invocation. Turns properly formatted input into an instance of Values * suitable for service invocation. Called before service invocation to provide input. If the * transport is HTTP, and a Content-Encoding header was specified with the value "gzip" or "deflate", * the stream is wrapped in a decompression stream. * * @param contentHandlerInput The input arguments for processing by the content handler. * @throws IOException If an error occurs writing to the output stream. */ public void getInputValues(ContentHandlerInput contentHandlerInput) throws IOException { if (startable.isStarted()) { String contentType = contentHandlerInput.getInvokeState().getContentType(); if (!(contentType != null && EXCLUDED_CONTENT_TYPES.matcher(contentType).matches())) { ProtocolInfoIf protocolInfoIf = contentHandlerInput.getInvokeState().getProtocolInfoIf(); if (protocolInfoIf instanceof HTTPState) { HTTPState httpState = (HTTPState)protocolInfoIf; String contentEncoding = HTTPStateHelper.getHeader(httpState, HttpHeader.CONTENT_ENCODING); if (contentEncoding != null) { if (contentEncoding.equalsIgnoreCase("gzip")) { contentHandlerInput.setInputStream(new GZIPInputStream(contentHandlerInput.getInputStream(), InputOutputHelper.DEFAULT_BUFFER_SIZE)); HTTPStateHelper.removeHeader(httpState, HttpHeader.CONTENT_ENCODING); } else if (contentEncoding.equalsIgnoreCase("deflate")) { contentHandlerInput.setInputStream(new DeflaterInputStream(contentHandlerInput.getInputStream())); HTTPStateHelper.removeHeader(httpState, HttpHeader.CONTENT_ENCODING); } } } } } }
/** * @tests DeflaterInputStream#available() */ public void testAvailable() throws IOException { byte[] buf = new byte[1024]; DeflaterInputStream dis = new DeflaterInputStream(is); assertEquals(1, dis.available()); assertEquals(120, dis.read()); assertEquals(1, dis.available()); assertEquals(22, dis.read(buf, 0, 1024)); assertEquals(1, dis.available()); assertEquals(-1, dis.read()); assertEquals(0, dis.available()); dis.close(); try { dis.available(); fail("should throw IOException"); } catch (IOException e) { // expected } }
/** * @tests DeflaterInputStream#read() */ public void testRead() throws IOException { DeflaterInputStream dis = new DeflaterInputStream(is); assertEquals(1, dis.available()); assertEquals(120, dis.read()); assertEquals(1, dis.available()); assertEquals(156, dis.read()); assertEquals(1, dis.available()); assertEquals(243, dis.read()); assertEquals(1, dis.available()); dis.close(); try { dis.read(); fail("should throw IOException"); } catch (IOException e) { // expected } }
/** Writes an entry with the given name, date and external file attributes from the buffer. */ private void writeEntryFromBuffer(ZipFileEntry entry, byte[] uncompressed) throws IOException { CRC32 crc = new CRC32(); crc.update(uncompressed); entry.setCrc(crc.getValue()); entry.setSize(uncompressed.length); if (mode == OutputMode.FORCE_STORED) { entry.setMethod(Compression.STORED); entry.setCompressedSize(uncompressed.length); writeEntry(entry, new ByteArrayInputStream(uncompressed)); } else { ByteArrayOutputStream compressed = new ByteArrayOutputStream(); copyStream(new DeflaterInputStream(new ByteArrayInputStream(uncompressed), getDeflater()), compressed); entry.setMethod(Compression.DEFLATED); entry.setCompressedSize(compressed.size()); writeEntry(entry, new ByteArrayInputStream(compressed.toByteArray())); } }
/** * Get a replay cued up for the 'content' (after all leading headers) * * @return A replay input stream. * @throws IOException */ public InputStream getContentReplayInputStream() throws IOException { InputStream entityStream = getEntityReplayInputStream(); if(StringUtils.isEmpty(contentEncoding)) { return entityStream; } else if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) { try { return new GZIPInputStream(entityStream); } catch (IOException ioe) { logger.log(Level.WARNING,"gzip problem; using raw entity instead",ioe); IOUtils.closeQuietly(entityStream); // close partially-read stream return getEntityReplayInputStream(); } } else if ("deflate".equalsIgnoreCase(contentEncoding)) { return new DeflaterInputStream(entityStream); } else if ("identity".equalsIgnoreCase(contentEncoding) || "none".equalsIgnoreCase(contentEncoding)) { return entityStream; } else { // shouldn't be reached given check on setContentEncoding logger.log(Level.INFO,"Unknown content-encoding '"+contentEncoding+"' declared; using raw entity instead"); return entityStream; } }
@SuppressWarnings("restriction") @Override public void writeImpl() { final ByteArrayInputStream bais = new ByteArrayInputStream(this.packet); final DeflaterInputStream deflaterInputStream = new DeflaterInputStream(bais); byte[] defos; try { defos = sun.misc.IOUtils.readFully(deflaterInputStream, -1, true); writeD(defos.length); writeB(defos); } catch (final IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void setValue(InputStreamHolder is, BlobFactory aBlobFactory) { deserialBlob = null; try (DeflaterInputStream inputStream = new DeflaterInputStream(is.getInputStream())) { setBlob(aBlobFactory.createBlob(inputStream)); } catch (Exception e) { String msg = "Impossible de mettre à jour le blob"; throw new RuntimeException(msg, e); } }
private static byte[] compress(byte[] data) { byte[] ret; try (ByteArrayInputStream bis = new ByteArrayInputStream(data); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { ByteStreams.copy(new DeflaterInputStream(bis), bos); ret = bos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } return ret; }
private InputStream getInputStream(HttpURLConnection urlConn) throws IOException { InputStream is = urlConn.getInputStream(); String contentEncoding = urlConn.getContentEncoding(); if (contentEncoding == null) { return is; } if ("gzip".equalsIgnoreCase(contentEncoding)) { return new GZIPInputStream(is); } if ("deflate".equalsIgnoreCase(contentEncoding)) { return new DeflaterInputStream(is); } // This should never happen throw new IOException("Unsupported content encoding " + contentEncoding); }
public static byte[] decompress(byte[] contentBytes) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { InputStream input = new DeflaterInputStream(new ByteArrayInputStream(contentBytes)); byte[] buf = new byte[1024]; int len = -1; while ((len = input.read(buf)) > 0) { out.write(buf, 0, len); } } catch (IOException e) { throw new RuntimeException(e); } return out.toByteArray(); }
void prepareForWriting() { IOUtils.closeQuietly(dataWriter); setItem(COSName.N, asDirectObject(COSInteger.get(counter))); setItem(COSName.FIRST, asDirectObject(COSInteger.get(header.size()))); setItem(COSName.FILTER, asDirectObject(COSName.FLATE_DECODE)); this.filtered = new DeflaterInputStream( new SequenceInputStream(new ByteArrayInputStream(header.toByteArray()), new ByteArrayInputStream(data.toByteArray()))); this.header = null; this.data = null; }
/** * @tests DeflaterInputStream#mark() */ public void testMark() throws IOException { // mark do nothing DeflaterInputStream dis = new DeflaterInputStream(is); dis.mark(-1); dis.mark(0); dis.mark(1); dis.close(); dis.mark(1); }
/** * @tests DeflaterInputStream#markSupported() */ public void testMarkSupported() throws IOException { DeflaterInputStream dis = new DeflaterInputStream(is); assertFalse(dis.markSupported()); dis.close(); assertFalse(dis.markSupported()); }
/** * @tests DeflaterInputStream#DeflaterInputStream(InputStream) */ public void testDeflaterInputStreamInputStream() { // ok new DeflaterInputStream(is); // fail try { new DeflaterInputStream(null); fail("should throw NullPointerException"); } catch (NullPointerException e) { // expected } }
/** * Writes an entry from the specified source {@link ZipReader} and {@link ZipFileEntry} using the * specified {@link EntryAction}. * * <p>Writes the output entry from the input entry performing inflation or deflation as needed * and applies any values from the {@link EntryAction} as needed. */ private void writeEntry(ZipReader zip, ZipFileEntry entry, EntryAction action) throws IOException { checkArgument(action.getType() != ActionType.SKIP, "Cannot write a zip entry whose action is of type SKIP."); ZipFileEntry outEntry = new ZipFileEntry(entry); if (action.getType() == ActionType.RENAME) { checkNotNull(action.getNewName(), "ZipEntryFilter actions of type RENAME must not have a null filename."); outEntry.setName(action.getNewName()); } if (action.getDate() != null) { outEntry.setTime(action.getDate().getTime()); } InputStream data; if (mode == OutputMode.FORCE_DEFLATE && entry.getMethod() != Compression.DEFLATED) { // The output mode is deflate, but the entry compression is not. Create a deflater stream // from the raw file data and deflate to a temporary byte array to determine the deflated // size. Then use this byte array as the input stream for writing the entry. ByteArrayOutputStream tmp = new ByteArrayOutputStream(); copyStream(new DeflaterInputStream(zip.getRawInputStream(entry), getDeflater()), tmp); data = new ByteArrayInputStream(tmp.toByteArray()); outEntry.setMethod(Compression.DEFLATED); outEntry.setCompressedSize(tmp.size()); } else if (mode == OutputMode.FORCE_STORED && entry.getMethod() != Compression.STORED) { // The output mode is stored, but the entry compression is not; create an inflater stream // from the raw file data. data = new InflaterInputStream(zip.getRawInputStream(entry), getInflater()); outEntry.setMethod(Compression.STORED); outEntry.setCompressedSize(entry.getSize()); } else { // Entry compression agrees with output mode; use the raw file data as is. data = zip.getRawInputStream(entry); } writeEntry(outEntry, data); }
private ServletRequest wrapDeflatedRequest(HttpServletRequest request) throws IOException { final Deflater deflater = buildDeflater(); final DeflaterInputStream input = new DeflaterInputStream(request.getInputStream(), deflater, _bufferSize) { @Override public void close() throws IOException { deflater.reset(); localDeflater.set(deflater); super.close(); } }; return new WrappedServletRequest(request, input); }
public DeflaterInputStream decompressor(InputStream in) throws IOException { return new DeflaterInputStream(in); }
@Test public void embeddedFileIsCompressed() throws Exception { victim = ReadOnlyFilteredCOSStream.readOnlyEmbeddedFile(StreamSource.newInstance(stream, "chuck")); assertThat(victim.getFilteredStream(), instanceOf(DeflaterInputStream.class)); }
/** * Construct new NbtInputStream for deflated input stream and limiter. * * @param in input stream to be used. * @param limiter limiter to be used. * * @return new NbtInputStream. */ public static NbtInputStream fromDeflater(final InputStream in, final NbtLimiter limiter) { return new NbtInputStream(new DataInputStream(new BufferedInputStream(new DeflaterInputStream(new NbtInputLimitedStream(in, limiter))))); }