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; }
static Type determineType(final byte first, final byte second) { if (first == ZLIB.first()) { // zlib's second byte is for flags and a checksum - // make sure it is positive. int secondInt = ZLIB.second(); if (second < 0) { secondInt += 256; } // the second byte is not constant for zlib, it // differs based on compression level used and whether // a dictionary is used. What the RFC guarantees is // that "CMF and FLG, when viewed as a 16-bit unsigned // integer stored in MSB order (CMF*256 + FLG), is a // multiple of 31" if ((256 * first + secondInt) % 31 == 0) { return ZLIB; } else { return UNSUPPORTED; } } else if (first == GZIP.first()) { if (second == GZIP.second()) { return GZIP; } else { return UNSUPPORTED; } } else if (first == CHUNKED.first()) { if (second == CHUNKED.second()) { return CHUNKED; } else { return UNSUPPORTED; } } // by default assume the payload to be "raw, uncompressed" GELF, parsing will fail if it's // malformed. return UNCOMPRESSED; }
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; }