Example usage for java.nio CharBuffer flip

List of usage examples for java.nio CharBuffer flip

Introduction

In this page you can find the example usage for java.nio CharBuffer flip.

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:org.paxle.core.doc.impl.AParserDocumentTest.java

protected void appendData(File source, IParserDocument target) throws IOException {
    final StringBuilder sourceText = new StringBuilder();

    // writing Data
    final CharBuffer buffer = CharBuffer.allocate(50);
    final InputStreamReader sourceReader = new InputStreamReader(new FileInputStream(source),
            Charset.forName("UTF-8"));
    while (sourceReader.read(buffer) != -1) {
        buffer.flip();
        sourceText.append(buffer);/*from w  ww.  j ava 2s .c o m*/
        target.append(buffer.toString());
        buffer.clear();
    }

    sourceReader.close();
    target.flush();
}

From source file:uk.ac.ebi.arrayexpress.servlets.HttpProxyServlet.java

@Override
protected void doRequest(HttpServletRequest request, HttpServletResponse response, RequestType requestType)
        throws ServletException, IOException {
    RegexHelper MATCH_URL_REGEX = new RegexHelper("/+(.+)", "i");
    RegexHelper TEST_HOST_IN_URL_REGEX = new RegexHelper("^http\\:/{2}([^/]+)/", "i");
    RegexHelper SQUARE_BRACKETS_REGEX = new RegexHelper("\\[\\]", "g");

    logRequest(logger, request, requestType);

    String url = MATCH_URL_REGEX.matchFirst(request.getPathInfo());
    url = url.replaceFirst("http:/{1,2}", "http://"); // stupid hack as tomcat 6.0 removes second forward slash
    String queryString = request.getQueryString();

    if (0 < url.length()) {
        if (!TEST_HOST_IN_URL_REGEX.test(url)) { // no host here, will self
            url = "http://localhost:" + String.valueOf(request.getLocalPort()) + "/" + url;
        }/*  w  w w. j ava  2 s  . c om*/
        logger.debug("Will access [{}]", url);

        GetMethod getMethod = new GetMethod(url);

        if (null != queryString) {
            queryString = SQUARE_BRACKETS_REGEX.replace(queryString, "%5B%5D");
            getMethod.setQueryString(queryString);
        }

        Enumeration requestHeaders = request.getHeaderNames();
        while (requestHeaders.hasMoreElements()) {
            String name = (String) requestHeaders.nextElement();
            String value = request.getHeader(name);
            if (null != value) {
                getMethod.setRequestHeader(name, value);
            }
        }

        try {
            httpClient.executeMethod(getMethod);

            int statusCode = getMethod.getStatusCode();
            long contentLength = getMethod.getResponseContentLength();
            logger.debug("Got response [{}], length [{}]", statusCode, contentLength);

            Header[] responseHeaders = getMethod.getResponseHeaders();
            for (Header responseHeader : responseHeaders) {
                String name = responseHeader.getName();
                String value = responseHeader.getValue();
                if (null != name && null != value && !(name.equals("Server") || name.equals("Date")
                        || name.equals("Transfer-Encoding"))) {
                    response.setHeader(responseHeader.getName(), responseHeader.getValue());
                }
            }

            if (200 != statusCode) {
                response.setStatus(statusCode);
            }

            InputStream inStream = getMethod.getResponseBodyAsStream();
            if (null != inStream) {
                BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));

                CharBuffer buffer = CharBuffer.allocate(PROXY_BUFFER_SIZE);
                while (in.read(buffer) >= 0) {
                    buffer.flip();
                    out.append(buffer);
                    buffer.clear();
                }

                in.close();
                out.close();
            }
        } catch (Exception x) {
            if (x.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException")) {
                logger.warn("Client aborted connection");
            } else {
                logger.error("Caught an exception:", x);
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, x.getMessage());
            }
        } finally {
            getMethod.releaseConnection();
        }
    }
}