Example usage for org.apache.http.util CharArrayBuffer clear

List of usage examples for org.apache.http.util CharArrayBuffer clear

Introduction

In this page you can find the example usage for org.apache.http.util CharArrayBuffer clear.

Prototype

public void clear() 

Source Link

Usage

From source file:org.vietspider.net.apache.DefaultResponseParser.java

public static Header[] parseHeaders(final SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen,
        LineParser parser) throws HttpException, IOException {

    if (inbuffer == null) {
        throw new IllegalArgumentException("Session input buffer may not be null");
    }//  www.  j a  va2  s.  com

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    ArrayList<CharArrayBuffer> headerLines = new ArrayList<CharArrayBuffer>();

    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (;;) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int i = 0;
            while (i < current.length()) {
                char ch = current.charAt(i);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                i++;
            }
            if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, i, current.length() - i);
        } else {
            headerLines.add(current);
            previous = current;
            current = null;
        }

        if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }

    Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++) {
        CharArrayBuffer buffer = headerLines.get(i);
        try {
            headers[i] = parser.parseHeader(buffer);
        } catch (ParseException ex) {
            throw new ProtocolException(ex.getMessage());
        }
    }
    return headers;
}

From source file:com.ok2c.lightmtp.impl.protocol.TestSMTPInOutBuffers.java

@Test
public void testReadLineChunks() throws Exception {
    SessionInputBuffer inbuf = new SMTPInputBuffer(16, 16);

    ReadableByteChannel channel = newChannel("O\ne\r\nTwo\r\nTh\ree");

    inbuf.fill(channel);//from   w  w w . j a v  a2s  . c om

    CharArrayBuffer line = new CharArrayBuffer(64);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("O", line.toString());
    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("e", line.toString());

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("Two", line.toString());

    line.clear();
    Assert.assertFalse(inbuf.readLine(line, false));

    channel = newChannel("\r\nFour");
    inbuf.fill(channel);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("Th\ree", line.toString());

    inbuf.fill(channel);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, true));
    Assert.assertEquals("Four", line.toString());

    line.clear();
    Assert.assertFalse(inbuf.readLine(line, true));
}

From source file:android.core.HttpHeaderTest.java

/**
 * Tests that cache control header supports multiple instances of the header,
 * according to HTTP specification./*from w w  w .j a  v  a 2s. c om*/
 *
 * The HTTP specification states the following about the fields:
 * Multiple message-header fields with the same field-name MAY be present
 * in a message if and only if the entire field-value for that header field
 * is defined as a comma-separated list [i.e., #(values)]. It MUST be
 * possible to combine the multiple header fields into one "field-name:
 * field-value" pair, without changing the semantics of the message, by
 * appending each subsequent field-value to the first, each separated by a
 * comma. The order in which header fields with the same field-name are
 * received is therefore significant to the interpretation of the combined
 * field value, and thus a proxy MUST NOT change the order of these field
 * values when a message is forwarded.
 */
public void testCacheControl() throws Exception {
    Headers h = new Headers();
    CharArrayBuffer buffer = new CharArrayBuffer(64);

    buffer.append(CACHE_CONTROL_MAX_AGE);
    h.parseHeader(buffer);

    buffer.clear();
    buffer.append(LAST_MODIFIED);
    h.parseHeader(buffer);
    assertEquals("max-age=15", h.getCacheControl());

    buffer.clear();
    buffer.append(CACHE_CONTROL_PRIVATE);
    h.parseHeader(buffer);
    assertEquals("max-age=15,private", h.getCacheControl());
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Parse HTTP headers and add them to a IHttpMessageBuilder until a non-header line is encountered.
 * //  w w  w .  j av a 2  s  . c om
 * @param parser HC line parser.
 * @param builder IHttpMessageBuilder to add parsed headers to.
 * @param buf Buffer containing header data.
 * @param bufCursor Parser cursor for buf. Adjusted to one character past the end of headers and optional CRLF line.
 */
protected void parseHeaders(final LineParser parser, final IHttpMessageBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    while (true) {
        lnBuf.clear();
        int idxPos = bufCursor.getPos();
        if (readLineHeader(buf, bufCursor, lnBuf) > 0) {
            try {
                // REVISIT don't want an extra step
                Header header = parser.parseHeader(lnBuf);
                builder.addHeader(header.getName(), header.getValue());
            } catch (ParseException e) {
                // for now we'll move the cursor back so the line gets treated as the start of the body
                bufCursor.updatePos(idxPos);
                return;
            }
        } else {
            break;
        }
    }
}

From source file:android.core.HttpHeaderTest.java

public void testCacheControlMultipleArguments() throws Exception {
    // get private method CacheManager.parseHeaders()
    Method m = CacheManager.class.getDeclaredMethod("parseHeaders",
            new Class[] { int.class, Headers.class, String.class });
    m.setAccessible(true);/*from  w  w  w.  ja  v  a 2 s . c  o  m*/

    // create indata
    Headers h = new Headers();
    CharArrayBuffer buffer = new CharArrayBuffer(64);
    buffer.append(CACHE_CONTROL_COMPOUND);
    h.parseHeader(buffer);

    CacheResult c = (CacheResult) m.invoke(null, 200, h, "text/html");

    // Check that expires is set to 0, to ensure that no-cache has overridden
    // the max-age argument
    assertEquals(0, c.getExpires());

    // check reverse order
    buffer.clear();
    buffer.append(CACHE_CONTROL_COMPOUND2);
    h.parseHeader(buffer);

    c = (CacheResult) m.invoke(null, 200, h, "text/html");
    assertEquals(0, c.getExpires());
}

From source file:org.mycard.net.network.AndroidHttpClientConnection.java

/***
 * Parses the response headers and adds them to the
 * given {@code headers} object, and returns the response StatusLine
 * @param headers store parsed header to headers.
 * @throws IOException/*from   w ww.j  a v a2 s. c om*/
 * @return StatusLine
 * @see HttpClientConnection#receiveResponseHeader()
  */
public StatusLine parseResponseHeader(Headers headers) throws IOException, ParseException {
    assertOpen();

    CharArrayBuffer current = new CharArrayBuffer(64);

    if (inbuffer.readLine(current) == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }

    // Create the status line from the status string
    StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(current,
            new ParserCursor(0, current.length()));

    //if (HttpLog.LOGV) HttpLog.v("read: " + statusline);
    int statusCode = statusline.getStatusCode();

    // Parse header body
    CharArrayBuffer previous = null;
    int headerNumber = 0;
    while (true) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            // This must be he buffer used to parse the status
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        char first = current.charAt(0);
        if ((first == ' ' || first == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int start = 0;
            int length = current.length();
            while (start < length) {
                char ch = current.charAt(start);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                start++;
            }
            if (maxLineLength > 0 && previous.length() + 1 + current.length() - start > maxLineLength) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, start, current.length() - start);
        } else {
            if (previous != null) {
                headers.parseHeader(previous);
            }
            headerNumber++;
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }

    if (previous != null) {
        headers.parseHeader(previous);
    }

    if (statusCode >= 200) {
        this.metrics.incrementResponseCount();
    }
    return statusline;
}