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

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

Introduction

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

Prototype

public String toString() 

Source Link

Usage

From source file:Main.java

public static String gzipToString(final HttpEntity entity, final String defaultCharset)
        throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }//w  w w  .j  a va2 s.  c o m
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    // gzip logic start
    if (entity.getContentEncoding().getValue().contains("gzip")) {
        instream = new GZIPInputStream(instream);
    }
    // gzip logic end
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    int i = (int) entity.getContentLength();
    if (i < 0) {
        i = 4096;
    }
    String charset = EntityUtils.getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    CharArrayBuffer buffer = new CharArrayBuffer(i);
    try {
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java

/**
 * This method takes in a ContentType and returns an equivalent ContentType, only overriding the
 * charset. This is needed because ContentType.withCharset(charset) does not correctly handle
 * headers with multiple parameters. Parsing a ContentType from a String works, calling
 * toString() to get the correct header value works, but there's no way from ContentType itself
 * to update a specific parameter in-place.
 *//*from ww w . j a  v  a2s .  co  m*/
public static ContentType setCharset(ContentType contentType, Charset charset)
        throws ParseException, UnsupportedCharsetException {
    // Get the correct header value
    String contentTypeString = contentType.toString();

    // Parse the header manually the same way ContentType does it
    CharArrayBuffer buffer = new CharArrayBuffer(contentTypeString.length());
    buffer.append(contentTypeString);
    ParserCursor cursor = new ParserCursor(0, contentTypeString.length());
    HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buffer, cursor);

    if (ArrayUtils.isNotEmpty(elements)) {
        String mimeType = elements[0].getName();
        NameValuePair[] params = elements[0].getParameters();
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
        boolean charsetFound = false;

        // Iterate through each parameter and override the charset if present
        if (ArrayUtils.isNotEmpty(params)) {
            for (NameValuePair nvp : params) {
                if (nvp.getName().equalsIgnoreCase("charset")) {
                    charsetFound = true;
                    nvp = new BasicNameValuePair(nvp.getName(), charset.name());
                }
                paramsList.add(nvp);
            }
        }

        // Add the charset at the end if it wasn't found before
        if (!charsetFound) {
            paramsList.add(new BasicNameValuePair("charset", charset.name()));
        }

        // Format the header the same way ContentType does it
        CharArrayBuffer newBuffer = new CharArrayBuffer(64);
        newBuffer.append(mimeType);
        newBuffer.append("; ");
        BasicHeaderValueFormatter.INSTANCE.formatParameters(newBuffer,
                paramsList.toArray(new NameValuePair[paramsList.size()]), false);
        // Once we have the correct string, let ContentType do the rest
        return ContentType.parse(newBuffer.toString());
    } else {
        throw new ParseException("Invalid content type: " + contentTypeString);
    }
}

From source file:com.llkj.cm.restfull.network.NetworkConnection.java

/**
 * Transform an InputStream into a String
 * /*ww  w . ja v a 2s .  c o m*/
 * @param is InputStream
 * @return String from the InputStream
 * @throws IOException If a problem occurs while reading the InputStream
 */
private static String convertStreamToString(final InputStream is, final boolean isGzipEnabled, final int method,
        final int contentLength) throws IOException {
    InputStream cleanedIs = is;
    if (isGzipEnabled) {
        cleanedIs = new GZIPInputStream(is);
    }

    try {
        switch (method) {
        case METHOD_GET:
        case METHOD_DELETE: {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(cleanedIs));
            final StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            return sb.toString();
        }
        case METHOD_POST:
        case METHOD_PUT: {
            int i = contentLength;
            if (i < 0) {
                i = 4096;
            }

            final Reader reader = new InputStreamReader(cleanedIs);
            final CharArrayBuffer buffer = new CharArrayBuffer(i);
            final char[] tmp = new char[1024];
            int l;
            while ((l = reader.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }

            return buffer.toString();
        }
        default:
            return null;
        }
    } finally {
        cleanedIs.close();

        if (isGzipEnabled) {
            is.close();
        }
    }
}

From source file:com.android.aft.AFNetworkConnection.AFNetworkConnection.java

/**
 * Transform an InputStream into a String
 *
 * @param is InputStream/*from  w ww. j ava 2s . co m*/
 * @return String from the InputStream
 * @throws IOException If a problem occurs while reading the InputStream
 */
public static String convertStreamToString(InputStream is, boolean isGzipEnabled, HttpMethod method,
        int contentLength) throws IOException {
    InputStream cleanedIs = is;
    if (isGzipEnabled) {
        cleanedIs = new GZIPInputStream(is);
    }

    try {
        switch (method) {
        case Get:
            final BufferedReader reader = new BufferedReader(new InputStreamReader(cleanedIs));
            final StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            return sb.toString();

        case Post:
            int i = contentLength;
            if (i < 0) {
                i = 4096;
            }

            final Reader readerPost = new InputStreamReader(cleanedIs);
            final CharArrayBuffer buffer = new CharArrayBuffer(i);
            final char[] tmp = new char[1024];
            int l;
            while ((l = readerPost.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }
            return buffer.toString();

        default:
            return null;
        }
    } finally {
        cleanedIs.close();

        if (isGzipEnabled) {
            is.close();
        }
    }
}

From source file:net.oneandone.sushi.fs.webdav.LoggingSessionOutputBuffer.java

@Override
public void writeLine(CharArrayBuffer buffer) throws IOException {
    // the buffer is *not* written via super.write ...
    logger.log(buffer.toString());
    super.writeLine(buffer);
    // CRLF is written via super.write, thus, we have proper logging
}

From source file:org.apache.droids.protocol.http.LenientHttpResponseParser.java

@Override
public Header parseHeader(CharArrayBuffer buffer) throws ParseException {
    try {/* www.  jav a2s .c om*/
        return super.parseHeader(buffer);
    } catch (ParseException ex) {
        // Suppress ParseException exception
        return new BasicHeader(buffer.toString(), null);
    }
}

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);/*ww w.j  ava 2 s .  co m*/

    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:com.jayway.restassured.internal.http.BasicNameValuePairWithNoValueSupport.java

public String toString() {
    // don't call complex default formatting for a simple toString

    if (this.value == null) {
        return name;
    } else {/*ww w.j a  v a2  s. c  o  m*/
        int len = this.name.length() + 1 + this.value.length();
        CharArrayBuffer buffer = new CharArrayBuffer(len);
        buffer.append(this.name);
        if (!noValueParam) {
            buffer.append("=");
            buffer.append(this.value);
        }
        return buffer.toString();
    }
}

From source file:com.grendelscan.commons.http.apache_overrides.serializable.SerializableNameValuePair.java

/**
 * Get a string representation of this pair.
 * //from  ww w.  ja v  a 2 s  .  c o m
 * @return A string representation.
 */
@Override
public String toString() {
    // don't call complex default formatting for a simple toString

    int len = name.length();
    if (value != null) {
        len += 1 + value.length();
    }
    CharArrayBuffer buffer = new CharArrayBuffer(len);

    buffer.append(name);
    if (value != null) {
        buffer.append("=");
        buffer.append(value);
    }
    return buffer.toString();
}

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

public String readLine() throws IOException {
    CharArrayBuffer charbuffer = new CharArrayBuffer(64);
    int l = readLine(charbuffer);
    return (l != -1) ? charbuffer.toString() : null;
}