1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4//  ------------------------------------------------------------------------
5//  All rights reserved. This program and the accompanying materials
6//  are made available under the terms of the Eclipse Public License v1.0
7//  and Apache License v2.0 which accompanies this distribution.
8//
9//      The Eclipse Public License is available at
10//      http://www.eclipse.org/legal/epl-v10.html
11//
12//      The Apache License v2.0 is available at
13//      http://www.opensource.org/licenses/apache2.0.php
14//
15//  You may elect to redistribute this code under either of these licenses.
16//  ========================================================================
17//
18
19package org.eclipse.jetty.server;
20
21import java.io.IOException;
22
23import javax.servlet.ServletInputStream;
24
25import org.eclipse.jetty.http.HttpParser;
26import org.eclipse.jetty.io.Buffer;
27import org.eclipse.jetty.io.EofException;
28
29public class HttpInput extends ServletInputStream
30{
31    protected final AbstractHttpConnection _connection;
32    protected final HttpParser _parser;
33
34    /* ------------------------------------------------------------ */
35    public HttpInput(AbstractHttpConnection connection)
36    {
37        _connection=connection;
38        _parser=(HttpParser)connection.getParser();
39    }
40
41    /* ------------------------------------------------------------ */
42    /*
43     * @see java.io.InputStream#read()
44     */
45    @Override
46    public int read() throws IOException
47    {
48        byte[] bytes = new byte[1];
49        int read = read(bytes, 0, 1);
50        return read < 0 ? -1 : 0xff & bytes[0];
51    }
52
53    /* ------------------------------------------------------------ */
54    /*
55     * @see java.io.InputStream#read(byte[], int, int)
56     */
57    @Override
58    public int read(byte[] b, int off, int len) throws IOException
59    {
60        int l=-1;
61        Buffer content=_parser.blockForContent(_connection.getMaxIdleTime());
62        if (content!=null)
63            l= content.get(b, off, len);
64        else if (_connection.isEarlyEOF())
65            throw new EofException("early EOF");
66        return l;
67    }
68
69    /* ------------------------------------------------------------ */
70    @Override
71    public int available() throws IOException
72    {
73        return _parser.available();
74    }
75}
76