Example usage for org.apache.http.message ParserCursor atEnd

List of usage examples for org.apache.http.message ParserCursor atEnd

Introduction

In this page you can find the example usage for org.apache.http.message ParserCursor atEnd.

Prototype

public boolean atEnd() 

Source Link

Usage

From source file:Main.java

/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed.
 *
 * @param s text to parse.//from  w  w w  .  j av  a  2 s.c  o m
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
        }
    }
    return list;
}

From source file:com.sina.cloudstorage.util.URLEncodedUtils.java

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding.//from   ww  w. j  a  v a 2  s.c  o m
 *
 * @param s
 *            text to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 *
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(decode(nvp.getName(), charset), decode(nvp.getValue(), charset)));
        }
    }
    return list;
}

From source file:com.android.idtt.http.client.util.URLEncodedUtils.java

/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding.//from ww  w  . j a v  a 2 s. c  o  m
 *
 * @param s       text to parse.
 * @param charset Encoding to use when decoding the parameters.
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}

From source file:com.gistlabs.mechanize.util.apache.URLEncodedUtils.java

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding./*from  w  w  w  .j a  v a  2 s  . co  m*/
 *
 * @param s
 *            text to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 *
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null)
        return Collections.emptyList();
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0)
            list.add(new BasicNameValuePair(decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
    }
    return list;
}

From source file:com.mcxiaoke.next.http.util.URLUtils.java

/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed from the given string using the given character
 * encoding.// ww  w .  j a va2s .  c  om
 *
 * @param s                  text to parse.
 * @param charset            Encoding to use when decoding the parameters.
 * @param parameterSeparator The characters used to separate parameters, by convention, {@code '&'} and {@code ';'}.
 * @return a list of {@link org.apache.http.NameValuePair} as built from the URI's query portion.
 * @since 4.3
 */
public static List<NameValuePair> parse(final String s, final Charset charset,
        final char... parameterSeparator) {
    if (s == null) {
        return Collections.emptyList();
    }
    final BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    final ParserCursor cursor = new ParserCursor(0, buffer.length());
    final List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        final NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, parameterSeparator);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}

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

/**
 * Get the next line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as valid
 * line delimiters. Treats the entire buffer as a line if no line delimiters are found.
 * /*from www . j  a va2 s  . c  o  m*/
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLine(final CharArrayBuffer src, final ParserCursor srcCursor, final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());
    int idxEnd;

    if (idxLf > 0) {
        if (src.charAt(idxLf - 1) == HTTP.CR) {
            idxEnd = idxLf - 1;
        } else {
            idxEnd = idxLf;
        }
    } else {
        idxEnd = srcCursor.getUpperBound();
        idxLf = idxEnd - 1;
    }

    dst.append(src, idxPos, idxEnd - idxPos);
    srcCursor.updatePos(idxLf + 1);
    return idxEnd - idxPos;
}

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

/**
 * Get the next header line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as
 * valid line delimiters. Treats the entire buffer as a line if no line delimiters are found. Supports folded header
 * field values as per the HTTP/1.1 specification.
 *    //www  .ja v a2s .  c o  m
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLineHeader(final CharArrayBuffer src, final ParserCursor srcCursor,
        final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int chCnt = 0;
    int idxLf, idxEnd;

    do {
        idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());

        if (idxLf > 0) {
            if (src.charAt(idxLf - 1) == HTTP.CR) {
                idxEnd = idxLf - 1;
            } else {
                idxEnd = idxLf;
            }
        } else {
            idxEnd = srcCursor.getUpperBound();
            idxLf = idxEnd - 1;
        }

        if (chCnt != 0) {
            while (idxPos < idxEnd && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP)) {
                idxPos++;
            }
            if (idxPos != idxEnd) {
                dst.append(' ');
            }
        }

        dst.append(src, idxPos, idxEnd - idxPos);
        chCnt += idxEnd - idxPos;
        idxPos = idxLf + 1;
        srcCursor.updatePos(idxPos);
    } while (idxPos < srcCursor.getUpperBound()
            && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP));

    return chCnt;
}

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

/**
 * Parse a HTTP response in a string./*from   ww  w  . jav a  2 s .  c  o m*/
 * 
 * @param content HTTP response string.
 * @return HttpResponse, or null if the given HTTP response was empty. 
 * @throws UnsupportedEncodingException
 */
public void parseResponse(final String content) throws UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseStatusLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

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

/**
 * Parse a manually-entered HTTP request into the IHttpRequestBuilder.
 * /*from  w ww.j av  a2 s .  c o  m*/
 * @param content Manually-entered HTTP request.
 */
public void parseRequest(final String content) throws URISyntaxException, UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseRequestLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public List<InetAddressRange> parseAll(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) throws ParseException, UnknownHostException {

    Args.notNull(buffer, "Char array buffer");
    Args.notNull(cursor, "Parser cursor");

    char[] delims = delimiters != null ? delimiters : COMMA;

    List<InetAddressRange> ranges = new ArrayList<InetAddressRange>();
    while (!cursor.atEnd()) {
        ranges.add(parse(buffer, cursor, delims));
        int pos = cursor.getPos();
        if (pos < cursor.getUpperBound() && isOneOf(buffer.charAt(pos), delims)) {
            cursor.updatePos(pos + 1);//  w  ww . j a v a  2 s.  c o m
        }
    }
    return ranges;
}