Java Code Examples for org.apache.http.util.CharArrayBuffer#toString()

The following examples show how to use org.apache.http.util.CharArrayBuffer#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientDownloader.java    From gecco with MIT License 6 votes vote down vote up
public String getContent(InputStream instream, long contentLength, String charset) throws IOException {
try {
	if (instream == null) {
           return null;
       }
       int i = (int)contentLength;
       if (i < 0) {
           i = 4096;
       }
       Reader reader = new InputStreamReader(instream, charset);
       CharArrayBuffer buffer = new CharArrayBuffer(i);
       char[] tmp = new char[1024];
       int l;
       while((l = reader.read(tmp)) != -1) {
           buffer.append(tmp, 0, l);
       }
       return buffer.toString();
} finally {
	Objects.requireNonNull(instream).reset();
}
      
  }
 
Example 2
Source File: LocalRestChannel.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void doSendResponse(RestResponse response) {
    status = response.status().getStatus();

    byte[] bytes = response.content().toBytes();
    long length = bytes.length;
    Args.check(length <= Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory");
    if(length < 0) {
        length = 4096;
    }

    InputStream instream =  new ByteArrayInputStream(bytes);
    InputStreamReader reader = new InputStreamReader(instream, Consts.UTF_8);
    CharArrayBuffer buffer = new CharArrayBuffer((int)length);
    char[] tmp = new char[1024];

    int l;
    try {
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        content = buffer.toString();
    } catch (IOException e) {
        status = RestStatus.INTERNAL_SERVER_ERROR.getStatus();
        content = "IOException: " + e.getMessage();
    } finally {
        try {
            reader.close();
            instream.close();
        } catch (IOException e1) {
            content = "IOException: " + e1.getMessage();
        } finally {
            count.countDown();
        }
    }
}
 
Example 3
Source File: CustomConnectionsHttpClientFactory.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public Header parseHeader(final CharArrayBuffer buffer) throws ParseException {
  try {
    return super.parseHeader(buffer);
  } catch (ParseException ex) {
    // Suppress ParseException exception
    return new BasicHeader("invalid", buffer.toString());
  }
}
 
Example 4
Source File: ReadLogString.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Docker logs contains header
 * [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}
 * STREAM_TYPE
 * 0: stdin (is written on stdout)
 * 1: stdout
 * 2: stderr
 *
 * SIZE1, SIZE2, SIZE3, SIZE4 are the four bytes of the uint32 size
 * encoded as big endian.
 *
 * This method do:
 *
 * 1) Read 8 bytes.
 * 2) Choose stdout or stderr depending on the first byte.
 * 3) Extract the frame size from the last four bytes.
 * 4) Read the extracted size and output it on the correct output.
 * 5) Goto 1.
 *
 * @param entity HttpEntity for read message.
 * @return Logs from container in String.
 * @throws IOException if the entity cannot be read
 */
private String toString(final HttpEntity entity) throws IOException {
    final InputStream instream = entity.getContent();
    final CharArrayBuffer buffer = new CharArrayBuffer(
            this.getCapacity(entity)
    );
    if (instream != null) {
        try {
            final Reader reader = new InputStreamReader(
                    instream,
                    this.getCharset(ContentType.get(entity))
            );
            this.read(buffer, reader);
        } finally {
            instream.close();
        }
    }
    return buffer.toString();
}
 
Example 5
Source File: HTTPGet.java    From TVRemoteIME with GNU General Public License v2.0 4 votes vote down vote up
public static String readString(String uri){
    HTTPSTrustManager.allowAllSSL();
    try {
        URL url = new URL(uri);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
        conn.setRequestProperty("Accept-Encoding", "identity");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        int code = conn.getResponseCode();
        if (code == 200) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
            try {
                int length = conn.getContentLength();
                if (length < 0) {
                    length = 4096;
                }
                CharArrayBuffer buffer = new CharArrayBuffer(length);
                char[] tmp = new char[1024];

                int l;
                while ((l = reader.read(tmp)) != -1) {
                    buffer.append(tmp, 0, l);
                }
                return buffer.toString();
            }finally {

                reader.close();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        Log.e("HTTPGet", "readString", e);
    }
    finally {

    }
    return null;
}
 
Example 6
Source File: EntityUtils.java    From RoboZombie with Apache License 2.0 4 votes vote down vote up
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be deserialized
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        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;
        }
        Charset charset = null;
        try {
            ContentType contentType = ContentType.getOrDefault(entity);
            charset = contentType.getCharset();
        } catch (UnsupportedCharsetException ex) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
        }
        Reader reader = new InputStreamReader(instream, charset);
        CharArrayBuffer buffer = new CharArrayBuffer(i);
        char[] tmp = new char[1024];
        int l;
        while((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}