Java Code Examples for javax.servlet.ServletInputStream#readLine()

The following examples show how to use javax.servlet.ServletInputStream#readLine() . 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: TestRequestBodyCapturing.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setUp() {
    webConfiguration = tracer.getConfig(WebConfiguration.class);
    coreConfiguration = tracer.getConfig(CoreConfiguration.class);
    doReturn(CoreConfiguration.EventType.ALL).when(coreConfiguration).getCaptureBody();
    filterChain = new MockFilterChain(new HttpServlet() {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            final ServletInputStream is = req.getInputStream();
            int read;
            do {
                read = streamConsumer.read(is);
            } while (read != -1);
            streamCloser.close(is);
        }
    });
    streamConsumer = is -> is.readLine(BUFFER, 0, BUFFER.length);
    streamCloser = InputStream::close;

}
 
Example 2
Source File: HttpRequestFileUpload.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
private int waitingReadLine(ServletInputStream in, byte[] buf, int off, int len, int reqLen) throws IOException {
    int i = -1;

    while (((i = in.readLine(buf, off, len)) == -1) && (reqLen > 0)) {
        Debug.logInfo("waiting", module);
        if (waitCount > MAX_WAITS) {
            Debug.logInfo("waited " + waitCount + " times, bailing out while still expecting " +
                reqLen + " bytes.", module);
            throw new IOException("waited " + waitCount + " times, bailing out while still expecting " +
                    reqLen + " bytes.");
        }
        waitCount++;
        long endMS = new Date().getTime() + WAIT_INTERVAL;

        while (endMS > (new Date().getTime())) {
            try {
                wait(WAIT_INTERVAL);
            } catch (Exception e3) {
                Debug.logInfo(".", module);
            }
        }
        Debug.logInfo((new Date().getTime() - endMS) + " ms", module);
    }
    return i;
}
 
Example 3
Source File: EchoServlet.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    final ServletOutputStream os = resp.getOutputStream();
    final ServletInputStream is = req.getInputStream();
    switch (req.getParameter("read-method")) {
        case "read-byte":
            while ((read = is.read()) > 0) {
                os.write(read);
            }
            break;
        case "read-bytes":
            while ((read = is.read(buffer)) > 0) {
                os.write(buffer, 0, read);
            }
            break;
        case "read-offset":
            while ((read = is.read(buffer, 42, buffer.length / 2)) > 0) {
                os.write(buffer, 42, read);
            }
            break;
        case "read-line":
            while ((read = is.readLine(buffer, 0, buffer.length)) > 0) {
                os.write(buffer, 0, read);
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid read-method");
    }
    resp.setContentType(req.getContentType());
}
 
Example 4
Source File: RunCommand.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Reads a line into the specified byte array. The data is read until a newline
 * character is encountered (all of "\n", "\r", "\n\r", "\r\n" are considered a
 * newline character) or a maximum number of bytes is read. Thus the clients
 * should not expect the result to be always a complete line. The amximum number
 * of bytes is dictated by the length of the byte array.
 *
 * This wrapper is introduced in order not to return -1, when a line cannot be
 * read, but to throw an IOException instead, since this situation is erroneous
 * anyway.
 *
 * @param input
 *          A ServletInputStream from which to read data.
 * @param buffer
 *          A byte array where to put the data
 * @throws java.io.IOException
 *          If an I/O error happens.
 * @return
 *          The number of bytes read.
 */
private int read(ServletInputStream input, byte[] buffer) throws IOException {
    int length = input.readLine(buffer, 0, buffer.length);
    
    if (length == -1) {
        throw new IOException("Could not read a complete " +
                "buffer without reaching an EOL or reached " +
                "the end of stream prematurely");
    }
    
    return length;
}