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:cn.mimessage.and.sdk.net.request.DefaultValuePair.java

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

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

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

From source file:com.letv.mobile.core.rpc.http.util.ParserCursor.java

public String toString() {
    CharArrayBuffer buffer = new CharArrayBuffer(16);
    buffer.append('[');
    buffer.append(Integer.toString(this.lowerBound));
    buffer.append('>');
    buffer.append(Integer.toString(this.pos));
    buffer.append('>');
    buffer.append(Integer.toString(this.upperBound));
    buffer.append(']');
    return buffer.toString();
}

From source file:cn.aage.robot.http.entity.ContentType.java

/**
 * Generates textual representation of this content type which can be used as the value
 * of a <code>Content-Type</code> header.
 *///w w  w .j  a va  2  s . co m
@Override
public String toString() {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.append(this.mimeType);
    if (this.params != null) {
        buf.append("; ");
        BasicHeaderValueFormatter.DEFAULT.formatParameters(buf, this.params, false);
    } else if (this.charset != null) {
        buf.append("; charset=");
        buf.append(this.charset.name());
    }
    return buf.toString();
}

From source file:com.mcxiaoke.next.http.entity.ContentType.java

/**
 * Generates textual representation of this content type which can be used as the value
 * of a <code>Content-Type</code> header.
 *///from   www.  j av  a2s  . c om
@Override
public String toString() {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.append(this.mimeType);
    if (this.params != null) {
        buf.append("; ");
        BasicHeaderValueFormatter.INSTANCE.formatParameters(buf, this.params, false);
    } else if (this.charset != null) {
        buf.append("; charset=");
        buf.append(this.charset.name());
    }
    return buf.toString();
}

From source file:com.google.resting.component.impl.ServiceResponse.java

@Override
public String toString() {
    int length = 150;
    length = contentData.getContentLength() + length;
    for (Header header : responseHeaders) {
        length += header.getName().length() + header.getValue().length() + 3;
    }//from  w  w  w .j  a  v  a  2  s .  com
    CharArrayBuffer buffer = new CharArrayBuffer(length);
    buffer.append("\nServiceResponse\n---------------\nHTTP Status: ");
    buffer.append(statusCode);
    buffer.append("\nHeaders: \n");
    for (Header header : responseHeaders) {
        buffer.append(header.getName());
        buffer.append(" : ");
        buffer.append(header.getValue());
        buffer.append("\n");
    }

    buffer.append("Response body: \n");
    buffer.append(contentData);
    buffer.append("\n----------------\n");

    return buffer.toString();
}

From source file:at.general.solutions.android.ical.remote.HttpDownloadThread.java

@Override
public void run() {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, USER_AGENT);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    if (useAuthentication) {
        client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(remoteUsername, remotePassword));
    }//w w w. j  a va 2 s.c om

    HttpGet get = new HttpGet(remoteUrl);

    try {
        super.sendInitMessage(R.string.downloading);

        HttpResponse response = client.execute(get);
        Log.d(LOG_TAG, response.getStatusLine().getReasonPhrase() + " "
                + isGoodResponse(response.getStatusLine().getStatusCode()));
        if (isGoodResponse(response.getStatusLine().getStatusCode())) {
            HttpEntity entity = response.getEntity();

            InputStream instream = entity.getContent();
            if (instream == null) {
                super.sendErrorMessage(R.string.couldnotConnectToRemoteserver);
                return;
            }
            if (entity.getContentLength() > Integer.MAX_VALUE) {
                super.sendErrorMessage(R.string.remoteFileTooLarge);
                return;
            }
            int i = (int) entity.getContentLength();
            if (i < 0) {
                i = 4096;
            }
            String charset = EntityUtils.getContentCharSet(entity);
            if (charset == null) {
                charset = encoding;
            }
            if (charset == null) {
                charset = HTTP.DEFAULT_CONTENT_CHARSET;
            }
            Reader reader = new InputStreamReader(instream, charset);
            CharArrayBuffer buffer = new CharArrayBuffer(i);

            super.sendMaximumMessage(i);

            try {
                char[] tmp = new char[1024];
                int l;
                while ((l = reader.read(tmp)) != -1) {
                    buffer.append(tmp, 0, l);
                    super.sendProgressMessage(buffer.length());
                }
            } finally {
                reader.close();
            }

            super.sendFinishedMessage(buffer.toString());
        } else {
            int errorMsg = R.string.couldnotConnectToRemoteserver;
            if (isAccessDenied(response.getStatusLine().getStatusCode())) {
                errorMsg = R.string.accessDenied;
            } else if (isFileNotFound(response.getStatusLine().getStatusCode())) {
                errorMsg = R.string.remoteFileNotFound;
            }
            super.sendErrorMessage(errorMsg);
        }
    } catch (UnknownHostException e) {
        super.sendErrorMessage(R.string.unknownHostException, e);
        Log.e(LOG_TAG, "Error occured", e);
    } catch (Throwable e) {
        super.sendErrorMessage(R.string.couldnotConnectToRemoteserver, e);
        Log.e(LOG_TAG, "Error occured", e);
    }

    finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:im.delight.android.webrequest.WebRequest.java

protected String parseResponse(HttpResponse response) throws Exception {
    final Header contentEncoding = response.getFirstHeader("Content-Encoding");
    // if we have a compressed response (GZIP)
    if (contentEncoding != null && contentEncoding.getValue() != null
            && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        // get the entity and the content length (if any) from the response
        final HttpEntity entity = response.getEntity();
        long contentLength = entity.getContentLength();

        // handle too large or undefined content lengths
        if (contentLength > Integer.MAX_VALUE) {
            throw new Exception("Response too large");
        } else if (contentLength < 0) {
            // use an arbitrary buffer size
            contentLength = 4096;//from   ww w . j av a 2  s  . co m
        }

        // construct a GZIP input stream from the response
        InputStream responseStream = entity.getContent();
        if (responseStream == null) {
            return null;
        }
        responseStream = new GZIPInputStream(responseStream);

        // read from the stream
        Reader reader = new InputStreamReader(responseStream, mCharset);
        CharArrayBuffer buffer = new CharArrayBuffer((int) contentLength);
        try {
            char[] tmp = new char[1024];
            int l;
            while ((l = reader.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }
        } finally {
            reader.close();
        }

        // return the decompressed response text as a string
        return buffer.toString();
    }
    // if we have an uncompressed response
    else {
        // return the response text as a string
        return EntityUtils.toString(response.getEntity(), mCharset);
    }
}

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

/**
 * Read and parse the request line.//from w  w w.jav a  2  s .  com
 * 
 * @param parser HC LineParser.
 * @param builder HTTP request builder.
 * @param buf
 * @param bufCursor
 * @return
 * @throws URISyntaxException
 */
private int parseRequestLine(final LineParser parser, final IHttpRequestBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) throws URISyntaxException {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    if (readLine(buf, bufCursor, lnBuf) < 1) {
        // no data! 
        return -1;
    }
    final ParserCursor lnCursor = new ParserCursor(0, lnBuf.length());

    String method, uri;
    ProtocolVersion version;
    if ((method = nextWord(lnBuf, lnCursor)) != null) {
        if ((uri = nextWord(lnBuf, lnCursor)) != null) {
            try {
                version = parser.parseProtocolVersion(lnBuf, lnCursor);
            } catch (ParseException e) {
                // treat the unparseable version as HTTP/1.1
                version = new ProtocolVersion("HTTP", 1, 1);
            }
            ;
        } else {
            uri = "";
            version = null;//new ProtocolVersion("HTTP", 0, 9);
        }
    } else {
        method = lnBuf.toString();
        uri = "";
        version = null;//new ProtocolVersion("HTTP", 0, 9);
    }

    builder.setMethod(method);
    builder.setFromUri(new URI(uri));
    builder.setProtocolVersion(version);

    return 0;
}

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

@Override
public String toString() {
    CharArrayBuffer buffer = new CharArrayBuffer(64);
    buffer.append("[version: ");
    buffer.append(Integer.toString(cookieVersion));
    buffer.append("]");
    buffer.append("[name: ");
    buffer.append(name);/* w  w w.  j ava2s .  c om*/
    buffer.append("]");
    buffer.append("[name: ");
    buffer.append(value);
    buffer.append("]");
    buffer.append("[domain: ");
    buffer.append(cookieDomain);
    buffer.append("]");
    buffer.append("[path: ");
    buffer.append(cookiePath);
    buffer.append("]");
    buffer.append("[expiry: ");
    buffer.append(cookieExpiryDate);
    buffer.append("]");
    return buffer.toString();
}

From source file:me.xiaopan.android.gohttp.StringHttpResponseHandler.java

private String toString(HttpRequest httpRequest, final HttpEntity entity, final String defaultCharset)
        throws IOException, ParseException {
    InputStream inputStream = entity.getContent();
    if (inputStream == null) {
        return "";
    }/*from   www  .j a  va  2s.com*/
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
    }
    int contentLength = (int) entity.getContentLength();
    if (contentLength < 0) {
        contentLength = 4096;
    }
    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    long averageLength = contentLength / httpRequest.getProgressCallbackNumber();
    int callbackNumber = 0;
    Reader reader = new InputStreamReader(inputStream, charset);
    CharArrayBuffer buffer = new CharArrayBuffer(contentLength);
    HttpRequest.ProgressListener progressListener = httpRequest.getProgressListener();
    try {
        char[] tmp = new char[1024];
        int readLength;
        long completedLength = 0;
        while (!httpRequest.isStopReadData() && (readLength = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, readLength);
            completedLength += readLength;
            if (progressListener != null && !httpRequest.isCanceled()
                    && (completedLength >= (callbackNumber + 1) * averageLength
                            || completedLength == contentLength)) {
                callbackNumber++;
                new HttpRequestHandler.UpdateProgressRunnable(httpRequest, contentLength, completedLength)
                        .execute();
            }
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}