下面列出了怎么用org.apache.http.io.SessionInputBuffer的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Creates a new {@link SessionInputBuffer} bounded to a given maximum length.
*
* @param buffer the buffer to wrap
* @param length the maximum number of bytes to read (from the buffered stream).
*/
public BoundSessionInputBuffer(final SessionInputBuffer buffer, final long length) {
super(new HttpTransportMetricsImpl(), BUFFER_SIZE, 0, null, null);
this.bounded = new ContentLengthInputStream(buffer, length);
this.input = new CountingInputStream(this.bounded);
super.bind(this.input);
this.length = length;
}
public static String[] readCRLFSeparatedBlock(SessionInputBuffer input) throws IOException {
CharArrayBuffer line = new CharArrayBuffer(128);
List<String> ret = new ArrayList<>();
for (;;) {
if (input.readLine(line) == -1) break;
if (line.length() == 0) break;
ret.add(line.toString());
line.clear();
}
return ret.toArray(new String[ret.size()]);
}
public WarcRecord(SessionInputBuffer buffer) throws IOException {
this.warcHeaders = readCRLFSeparatedBlock(buffer);
if (this.warcHeaders.length == 0) throw new EOFException();
long contentLength = contentLength(this.warcHeaders);
if (contentLength == -1) throw new WarcFormatException("Can't find Content-Length");
final HeaderGroup hg = toHeaderGroup(this.warcHeaders);
date = WarcHeader.parseDate(WarcHeader.getFirstHeader(hg, WarcHeader.Name.WARC_DATE).getValue());
uuid = WarcHeader.parseId(WarcHeader.getFirstHeader(hg, WarcHeader.Name.WARC_RECORD_ID).getValue());
this.payload = new byte[(int)contentLength];
for (int read = 0; read < contentLength; read ++) this.payload[read] = (byte)buffer.read();
}
public HttpResponseWarcRecord(SessionInputBuffer buffer) throws IOException {
super(buffer);
buffer = warpSessionInputBuffer(new ByteArrayInputStream(this.payload));
this.headers = readCRLFSeparatedBlock(buffer);
long contentLength = contentLength(this.headers);
InputStream blockStream = new ContentLengthInputStream(buffer, contentLength);
this.entity = new byte[(int)contentLength];
blockStream.read(this.entity);
blockStream.close();
}
public GarbageAllergicHttpResponseParser(
SessionInputBuffer buffer,
LineParser lineParser,
HttpResponseFactory responseFactory,
MessageConstraints constraints
) {
super(buffer, lineParser, responseFactory, constraints);
}
@Override
protected HttpMessageParser<HttpResponse> createResponseParser(
final SessionInputBuffer buffer,
final HttpResponseFactory responseFactory,
final HttpParams params) {
return new DefaultHttpResponseParser(
buffer,
new MyLineParser(),
responseFactory,
params);
}
public static SessionInputBuffer warpSessionInputBuffer(final InputStream input) {
final SessionInputBufferImpl bufferImpl = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 1024, 0, null, null);
bufferImpl.bind(input);
return bufferImpl;
}
public InfoWarcRecord(SessionInputBuffer buffer) throws IOException {
super(buffer);
this.info = readCRLFSeparatedBlock(warpSessionInputBuffer(new ByteArrayInputStream(this.payload)));
}
public HttpRequestWarcRecord(SessionInputBuffer buffer) throws IOException {
super(buffer);
this.headers = readCRLFSeparatedBlock(warpSessionInputBuffer(new ByteArrayInputStream(this.payload)));
}
@Override
protected HttpMessageParser createResponseParser(final SessionInputBuffer buffer,
final HttpResponseFactory responseFactory, final HttpParams params) {
return new DefaultResponseParser(buffer, new IcyLineParser(), responseFactory, params);
}