Example usage for org.apache.http.util ByteArrayBuffer isEmpty

List of usage examples for org.apache.http.util ByteArrayBuffer isEmpty

Introduction

In this page you can find the example usage for org.apache.http.util ByteArrayBuffer isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Usage

From source file:de.rub.syssec.saaf.misc.ByteUtils.java

/**
 * Read a line from an inputstream into a byte buffer. A line might end with a LF or an CRLF. CR's are accepted inside a line and
 * are not understood as a beginning new line. This should work therefore on Mac OS X, Unix, Linux and Windows.
 * /*from   ww w  .ja va 2s .co m*/
 * See http://en.wikipedia.org/wiki/Newline for more.
 *  
 * @param in the inputstream
 * @param maxSize the maximum amount of bytes to read until a CRLF or LF is reached, a value of zero or smaller disables a limit (use w/ care!)
 * @return the buffer where read bytes are appended, this buffer will not contain any CR's or or CRLF's at the end of the array. Null is
 * returned if EOF is reached.
 * @throws IOException if something is wrong w/ the stream or the maxSize is reached
 */
public static byte[] parseLine(BufferedInputStream in, int maxSize) throws IOException {
    ByteArrayBuffer bab = new ByteArrayBuffer(512);
    int b;
    while (true) {
        if (!(maxSize <= 0 || bab.length() <= maxSize)) {
            throw new IOException("Maximal bytearraybuffer size of " + maxSize + " exceeded!");
        }
        b = in.read();
        if (b == -1) {
            if (bab.isEmpty()) {
                // we have nothing read yet and could nothing read, we will therefore return 'null' as this
                // indicates EOF.
                return null;
            } else {
                // return what we got so far
                return bab.toByteArray();
            }
        }
        // CRLF case
        if (b == '\r') { // check if we find a \n
            int next = in.read();
            if (b == -1) {
                // EOF; return what we got
                return bab.toByteArray();
            } else if (next == '\n') { // we did
                in.mark(-1); // rest mark
                return bab.toByteArray(); // return the line without CRLF
            } else {
                // found no CRLF but only a CR and some other byte, so we need to add both to the buffer and proceed
                bab.append('\r');
                bab.append(b);
            }
        }
        // LF case
        else if (b == '\n') { // we found a LF and therefore the end of a line
            return bab.toByteArray();
        } else { // we just found a byte which is happily appended
            bab.append(b);
        }
    }
}

From source file:com.vuze.android.remote.AndroidUtils.java

public static boolean readInputStreamIfStartWith(InputStream is, ByteArrayBuffer bab, byte[] startsWith)
        throws IOException {

    byte[] buffer = new byte[32 * 1024];

    boolean first = true;

    try {/*w ww  .  j a  v  a  2s .c o  m*/
        while (true) {

            int len = is.read(buffer);

            if (len <= 0) {

                break;
            }

            bab.append(buffer, 0, len);

            if (first) {
                first = false;
                for (int i = 0; i < startsWith.length; i++) {
                    if (startsWith[i] != buffer[i]) {
                        return false;
                    }
                }
            }
        }

        return bab.isEmpty() ? false : true;

    } finally {

        is.close();
    }
}

From source file:org.trancecode.http.BodypartResponseParser.java

public BodypartEntity parseBodypart(final boolean hasHeaders) throws IOException {
    if (hasHeaders) {
        parseHeaders();//from w  w w.ja v a  2s  .  c om
    }
    if (isBinary()) {
        if (headers.containsHeader(HttpHeaders.CONTENT_LENGTH)) {
            int length = Integer.parseInt(headers.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue());
            final byte[] in = new byte[length];
            int off = 0;
            while (length > 0) {
                final int lect = sessionBuffer.read(in, off, length);
                if (lect == -1) {
                    break;
                }
                off += lect;
                length -= lect;
            }
            sessionBuffer.readLine();
            sessionBuffer.readLine();
            return new BodypartEntity(new ByteArrayEntity(in), hasHeaders ? headers.copy() : null);
        } else {
            final ByteArrayBuffer buffer = new ByteArrayBuffer(40);
            final byte[] in = new byte[40];
            while (true) {
                final int lect = sessionBuffer.read(in, 0, 40);
                if (lect == -1) {
                    break;
                }
                buffer.append(in, 0, lect);
            }
            if (!buffer.isEmpty()) {
                sessionBuffer.readLine();
                sessionBuffer.readLine();
                return new BodypartEntity(new ByteArrayEntity(buffer.toByteArray()),
                        hasHeaders ? headers.copy() : null);
            }
        }
    } else {
        final String ch = partCharset == null ? charset : partCharset;
        final String mime = partContentType == null ? contentType : partContentType;
        final StringBuilder builder = new StringBuilder();
        boolean finish = false;
        do {
            final String line = sessionBuffer.readLine();
            if (line == null || StringUtils.startsWith(line, boundary)) {
                finish = true;
            } else {
                if (builder.length() > 0 && !isBinary()) {
                    builder.append("\n");
                }
                builder.append(line);
            }
        } while (!finish);
        if (builder.length() > 0) {
            return new BodypartEntity(new StringEntity(builder.toString(), mime, ch),
                    hasHeaders ? headers.copy() : null);
        }
    }
    return null;
}