public static boolean flushContent(MutableMessageHeader header, InputStream in, OutputStream out) throws MessageFormatException, IOException { boolean read = false; String te = header.getHeader(TRANSFER_ENCODING); if (CHUNKED.equalsIgnoreCase(te)) { in = new ChunkedInputStream(in); } else if (te != null) { throw new IOException("Unknown Transfer-Encoding '" + te + "'"); } else { String cl = header.getHeader(CONTENT_LENGTH); if (cl != null) { try { int l = Integer.parseInt(cl.trim()); if (l == 0) return read; in = new FixedLengthInputStream(in, l); } catch (NumberFormatException nfe) { throw new MessageFormatException("Invalid Content-Length header: " + cl, nfe); } } } byte[] buff = new byte[2048]; int got; while ((got = in.read(buff)) > 0) { read = true; if (out != null) out.write(buff, 0, got); } return read; }
public static InputStream encode(String codings, InputStream content) throws MessageFormatException { if (codings == null || codings.trim().equals("")) return content; String[] algos = codings.split("[ \t]*,[ \t]*"); if (algos.length == 1 && IDENTITY.equalsIgnoreCase(algos[0])) return content; for (int i = 0; i < algos.length; i++) { if (CHUNKED.equalsIgnoreCase(algos[i])) { content = new ChunkingInputStream(content); } else if (GZIP.equalsIgnoreCase(algos[i])) { content = new GzipInputStream(content); } else if (IDENTITY.equalsIgnoreCase(algos[i])) { // nothing to do } else throw new MessageFormatException("Unsupported coding : " + algos[i]); } return content; }