Java 类org.apache.http.impl.io.AbstractSessionInputBuffer 实例源码

项目:cameraserve    文件:SsdpAdvertiser.java   
private HttpMessage getHttpMessage(final byte[] data) {
    final HttpParams httpParams = new BasicHttpParams();
    final AbstractSessionInputBuffer inputBuffer = new AbstractSessionInputBuffer() {
        {
            init(new ByteArrayInputStream(data), 128, httpParams);
        }

        @Override
        public boolean isDataAvailable(int i) throws IOException {
            return this.hasBufferedData();
        }
    };
    final HttpRequestFactory msearchRequestFactory = new HttpRequestFactory() {
        @Override
        public HttpRequest newHttpRequest(RequestLine requestLine) throws MethodNotSupportedException {
            if (!requestLine.getMethod().equalsIgnoreCase("m-search"))
                throw new MethodNotSupportedException("Invalid method: " + requestLine.getMethod());
            if (!requestLine.getUri().equals("*"))
                throw new MethodNotSupportedException("Invalid URI: " + requestLine.getUri());

            return new BasicHttpRequest(requestLine);
        }

        @Override
        public HttpRequest newHttpRequest(String method, String uri) throws MethodNotSupportedException {
            if (!method.equalsIgnoreCase("m-search"))
                throw new MethodNotSupportedException("Invalid method: " + method);
            if (!uri.equals("*"))
                throw new MethodNotSupportedException("Invalid URI: " + uri);

            return new BasicHttpRequest(method, uri);
        }
    };

    HttpRequestParser requestParser = new HttpRequestParser(inputBuffer, null, msearchRequestFactory, httpParams);
    try {
        return requestParser.parse();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}