com.crashlytics.tools.android_2.1.0

EntitySerializer(ContentLengthStrategy lenStrategy)
  {
    this.lenStrategy = ((ContentLengthStrategy)Args.notNull(lenStrategy, "Content length strategy"));
  }
  
  protected OutputStream doSerialize(SessionOutputBuffer outbuffer, HttpMessage message)
    throws HttpException, IOException
  {
    long len = lenStrategy.determineLength(message);
    if (len == -2L) {
      return new ChunkedOutputStream(outbuffer);
    }
    if (len == -1L) {
      return new IdentityOutputStream(outbuffer);
    }
    return new ContentLengthOutputStream(outbuffer, len);
  }
  
  public void serialize(SessionOutputBuffer outbuffer, HttpMessage message, HttpEntity entity)
    throws HttpException, IOException
  {
    Args.notNull(outbuffer, "Session output buffer");
    Args.notNull(message, "HTTP message");
    Args.notNull(entity, "HTTP entity");
    OutputStream outstream = doSerialize(outbuffer, message);
    entity.writeTo(outstream);
    outstream.close();
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.entity.EntitySerializer
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.entity;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpException;
import org.apache.http.HttpMessage;
import org.apache.http.ParseException;
import org.apache.http.ProtocolException;
import org.apache.http.annotation.Immutable;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.util.Args;

@Immutable
public class LaxContentLengthStrategy
  implements ContentLengthStrategy
{
  public static final LaxContentLengthStrategy INSTANCE = new LaxContentLengthStrategy();
  private final int implicitLen;
  
  public LaxContentLengthStrategy(int implicitLen)
  {
    this.implicitLen = implicitLen;
  }
  
  public LaxContentLengthStrategy()
  {
    this(-1);
  }
  
  public long determineLength(HttpMessage message)
    throws HttpException
  {
    Args.notNull(message, "HTTP message");
    
    Header transferEncodingHeader = message.getFirstHeader("Transfer-Encoding");
    if (transferEncodingHeader != null)
    {
      HeaderElement[] encodings;
      try
      {
        encodings = transferEncodingHeader.getElements();
      }
      catch (ParseException px)
      {
        throw new ProtocolException("Invalid Transfer-Encoding header value: " + transferEncodingHeader, px);
      }
      int len = encodings.length;
      if ("identity".equalsIgnoreCase(transferEncodingHeader.getValue())) {
        return -1L;
      }
      if ((len > 0) && ("chunked".equalsIgnoreCase(encodings[(len - 1)].getName()))) {
        return -2L;
      }
      return -1L;
    }
    Header contentLengthHeader = message.getFirstHeader("Content-Length");
    if (contentLengthHeader != null)
    {
      long contentlen = -1L;
      Header[] headers = message.getHeaders("Content-Length");
      for (int i = headers.length - 1; i >= 0; i--)
      {
        Header header = headers[i];
        try
        {
          contentlen = Long.parseLong(header.getValue());
        }
        catch (NumberFormatException ignore) {}
      }
      if (contentlen >= 0L) {
        return contentlen;
      }
      return -1L;
    }
    return implicitLen;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.entity.LaxContentLengthStrategy
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.entity;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpMessage;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolException;
import org.apache.http.ProtocolVersion;
import org.apache.http.annotation.Immutable;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.util.Args;

@Immutable
public class StrictContentLengthStrategy
  implements ContentLengthStrategy
{
  public static final StrictContentLengthStrategy INSTANCE = new StrictContentLengthStrategy();
  private final int implicitLen;
  
  public StrictContentLengthStrategy(int implicitLen)
  {
    this.implicitLen = implicitLen;
  }
  
  public StrictContentLengthStrategy()
  {
    this(-1);
  }
  
  public long determineLength(HttpMessage message)
    throws HttpException
  {
    Args.notNull(message, "HTTP message");
    
    Header transferEncodingHeader = message.getFirstHeader("Transfer-Encoding");
    if (transferEncodingHeader != null)
    {
      String s = transferEncodingHeader.getValue();
      if ("chunked".equalsIgnoreCase(s))
      {
        if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
          throw new ProtocolException("Chunked transfer encoding not allowed for " + message.getProtocolVersion());
        }
        return -2L;
      }
      if ("identity".equalsIgnoreCase(s)) {
        return -1L;
      }
      throw new ProtocolException("Unsupported transfer encoding: " + s);
    }
    Header contentLengthHeader = message.getFirstHeader("Content-Length");
    if (contentLengthHeader != null)
    {
      String s = contentLengthHeader.getValue();
      try
      {
        long len = Long.parseLong(s);
        if (len < 0L) {
          throw new ProtocolException("Negative content length: " + s);
        }
        return len;
      }
      catch (NumberFormatException e)
      {
        throw new ProtocolException("Invalid content length: " + s);
      }
    }
    return implicitLen;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.entity.StrictContentLengthStrategy
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpMessage;
import org.apache.http.MessageConstraintException;
import org.apache.http.ParseException;
import org.apache.http.ProtocolException;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.config.MessageConstraints;
import org.apache.http.io.HttpMessageParser;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.LineParser;
import org.apache.http.params.HttpParamConfig;
import org.apache.http.params.HttpParams;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;

@NotThreadSafe
public abstract class AbstractMessageParser<T extends HttpMessage>
  implements HttpMessageParser<T>
{
  private static final int HEAD_LINE = 0;
  private static final int HEADERS = 1;
  private final SessionInputBuffer sessionBuffer;
  private final MessageConstraints messageConstraints;
  private final List<CharArrayBuffer> headerLines;
  protected final LineParser lineParser;
  private int state;
  private T message;
  
  @Deprecated
  public AbstractMessageParser(SessionInputBuffer buffer, LineParser parser, HttpParams params)
  {
    Args.notNull(buffer, "Session input buffer");
    Args.notNull(params, "HTTP parameters");
    sessionBuffer = buffer;
    messageConstraints = HttpParamConfig.getMessageConstraints(params);
    lineParser = (parser != null ? parser : BasicLineParser.INSTANCE);
    headerLines = new ArrayList();
    state = 0;
  }
  
  public AbstractMessageParser(SessionInputBuffer buffer, LineParser lineParser, MessageConstraints constraints)
  {
    sessionBuffer = ((SessionInputBuffer)Args.notNull(buffer, "Session input buffer"));
    this.lineParser = (lineParser != null ? lineParser : BasicLineParser.INSTANCE);
    messageConstraints = (constraints != null ? constraints : MessageConstraints.DEFAULT);
    headerLines = new ArrayList();
    state = 0;
  }
  
  public static Header[] parseHeaders(SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen, LineParser parser)
    throws HttpException, IOException
  {
    List<CharArrayBuffer> headerLines = new ArrayList();
    return parseHeaders(inbuffer, maxHeaderCount, maxLineLen, parser != null ? parser : BasicLineParser.INSTANCE, headerLines);
  }
  
  public static Header[] parseHeaders(SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen, LineParser parser, List<CharArrayBuffer> headerLines)
    throws HttpException, IOException
  {
    Args.notNull(inbuffer, "Session input buffer");
    Args.notNull(parser, "Line parser");
    Args.notNull(headerLines, "Header line list");
    
    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (;;)
    {
      if (current == null) {
        current = new CharArrayBuffer(64);
      } else {
        current.clear();
      }
      int l = inbuffer.readLine(current);
      if ((l == -1) || (current.length() < 1)) {
        break;
      }
      if (((current.charAt(0) == ' ') || (current.charAt(0) == '\t')) && (previous != null))
      {
        int i = 0;
        while (i < current.length())
        {
          char ch = current.charAt(i);
          if ((ch != ' ') && (ch != '\t')) {
            break;
          }
          i++;
        }
        if ((maxLineLen > 0) && (previous.length() + 1 + current.length() - i > maxLineLen)) {
          throw new MessageConstraintException("Maximum line length limit exceeded");
        }
        previous.append(' ');
        previous.append(current, i, current.length() - i);
      }
      else
      {
        headerLines.add(current);
        previous = current;
        current = null;
      }
      if ((maxHeaderCount > 0) && (headerLines.size() >= maxHeaderCount)) {
        throw new MessageConstraintException("Maximum header count exceeded");
      }
    }
    Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++)
    {
      CharArrayBuffer buffer = (CharArrayBuffer)headerLines.get(i);
      try
      {
        headers[i] = parser.parseHeader(buffer);
      }
      catch (ParseException ex)
      {
        throw new ProtocolException(ex.getMessage());
      }
    }
    return headers;
  }
  
  protected abstract T parseHead(SessionInputBuffer paramSessionInputBuffer)
    throws IOException, HttpException, ParseException;
  
  public T parse()
    throws IOException, HttpException
  {
    int st = state;
    switch (st)
    {
    case 0: 
      try
      {
        message = parseHead(sessionBuffer);
      }
      catch (ParseException px)
      {
        throw new ProtocolException(px.getMessage(), px);
      }
      state = 1;
    case 1: 
      Header[] headers = parseHeaders(sessionBuffer, messageConstraints.getMaxHeaderCount(), messageConstraints.getMaxLineLength(), lineParser, headerLines);
      
      message.setHeaders(headers);
      T result = message;
      message = null;
      headerLines.clear();
      state = 0;
      return result;
    }
    throw new IllegalStateException("Inconsistent parser state");
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.AbstractMessageParser
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpException;
import org.apache.http.HttpMessage;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.message.BasicLineFormatter;
import org.apache.http.message.LineFormatter;
import org.apache.http.params.HttpParams;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;

@NotThreadSafe
public abstract class AbstractMessageWriter<T extends HttpMessage>
  implements HttpMessageWriter<T>
{
  protected final SessionOutputBuffer sessionBuffer;
  protected final CharArrayBuffer lineBuf;
  protected final LineFormatter lineFormatter;
  
  @Deprecated
  public AbstractMessageWriter(SessionOutputBuffer buffer, LineFormatter formatter, HttpParams params)
  {
    Args.notNull(buffer, "Session input buffer");
    sessionBuffer = buffer;
    lineBuf = new CharArrayBuffer(128);
    lineFormatter = (formatter != null ? formatter : BasicLineFormatter.INSTANCE);
  }
  
  public AbstractMessageWriter(SessionOutputBuffer buffer, LineFormatter formatter)
  {
    sessionBuffer = ((SessionOutputBuffer)Args.notNull(buffer, "Session input buffer"));
    lineFormatter = (formatter != null ? formatter : BasicLineFormatter.INSTANCE);
    lineBuf = new CharArrayBuffer(128);
  }
  
  protected abstract void writeHeadLine(T paramT)
    throws IOException;
  
  public void write(T message)
    throws IOException, HttpException
  {
    Args.notNull(message, "HTTP message");
    writeHeadLine(message);
    for (HeaderIterator it = message.headerIterator(); it.hasNext();)
    {
      Header header = it.nextHeader();
      sessionBuffer.writeLine(lineFormatter.formatHeader(lineBuf, header));
    }
    lineBuf.clear();
    sessionBuffer.writeLine(lineBuf);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.AbstractMessageWriter
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import org.apache.http.Consts;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.BufferInfo;
import org.apache.http.io.HttpTransportMetrics;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.params.HttpParams;
import org.apache.http.util.Args;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.CharArrayBuffer;

@Deprecated
@NotThreadSafe
public abstract class AbstractSessionInputBuffer
  implements SessionInputBuffer, BufferInfo
{
  private InputStream instream;
  private byte[] buffer;
  private ByteArrayBuffer linebuffer;
  private Charset charset;
  private boolean ascii;
  private int maxLineLen;
  private int minChunkLimit;
  private HttpTransportMetricsImpl metrics;
  private CodingErrorAction onMalformedCharAction;
  private CodingErrorAction onUnmappableCharAction;
  private int bufferpos;
  private int bufferlen;
  private CharsetDecoder decoder;
  private CharBuffer cbuf;
  
  protected void init(InputStream instream, int buffersize, HttpParams params)
  {
    Args.notNull(instream, "Input stream");
    Args.notNegative(buffersize, "Buffer size");
    Args.notNull(params, "HTTP parameters");
    this.instream = instream;
    buffer = new byte[buffersize];
    bufferpos = 0;
    bufferlen = 0;
    linebuffer = new ByteArrayBuffer(buffersize);
    String charset = (String)params.getParameter("http.protocol.element-charset");
    this.charset = (charset != null ? Charset.forName(charset) : Consts.ASCII);
    ascii = this.charset.equals(Consts.ASCII);
    decoder = null;
    maxLineLen = params.getIntParameter("http.connection.max-line-length", -1);
    minChunkLimit = params.getIntParameter("http.connection.min-chunk-limit", 512);
    metrics = createTransportMetrics();
    CodingErrorAction a1 = (CodingErrorAction)params.getParameter("http.malformed.input.action");
    
    onMalformedCharAction = (a1 != null ? a1 : CodingErrorAction.REPORT);
    CodingErrorAction a2 = (CodingErrorAction)params.getParameter("http.unmappable.input.action");
    
    onUnmappableCharAction = (a2 != null ? a2 : CodingErrorAction.REPORT);
  }
  
  protected HttpTransportMetricsImpl createTransportMetrics()
  {
    return new HttpTransportMetricsImpl();
  }
  
  public int capacity()
  {
    return buffer.length;
  }
  
  public int length()
  {
    return bufferlen - bufferpos;
  }
  
  public int available()
  {
    return capacity() - length();
  }
  
  protected int fillBuffer()
    throws IOException
  {
    if (bufferpos > 0)
    {
      int len = bufferlen - bufferpos;
      if (len > 0) {
        System.arraycopy(buffer, bufferpos, buffer, 0, len);
      }
      bufferpos = 0;
      bufferlen = len;
    }
    int off = bufferlen;
    int len = buffer.length - off;
    int l = instream.read(buffer, off, len);
    if (l == -1) {
      return -1;
    }
    bufferlen = (off + l);
    metrics.incrementBytesTransferred(l);
    return l;
  }
  
  protected boolean hasBufferedData()
  {
    return bufferpos < bufferlen;
  }
  
  public int read()
    throws IOException
  {
    while (!hasBufferedData())
    {
      int noRead = fillBuffer();
      if (noRead == -1) {
        return -1;
      }
    }
    return buffer[(bufferpos++)] & 0xFF;
  }
  
  public int read(byte[] b, int off, int len)
    throws IOException
  {
    if (b == null) {
      return 0;
    }
    if (hasBufferedData())
    {
      int chunk = Math.min(len, bufferlen - bufferpos);
      System.arraycopy(buffer, bufferpos, b, off, chunk);
      bufferpos += chunk;
      return chunk;
    }
    if (len > minChunkLimit)
    {
      int read = instream.read(b, off, len);
      if (read > 0) {
        metrics.incrementBytesTransferred(read);
      }
      return read;
    }
    while (!hasBufferedData())
    {
      int noRead = fillBuffer();
      if (noRead == -1) {
        return -1;
      }
    }
    int chunk = Math.min(len, bufferlen - bufferpos);
    System.arraycopy(buffer, bufferpos, b, off, chunk);
    bufferpos += chunk;
    return chunk;
  }
  
  public int read(byte[] b)
    throws IOException
  {
    if (b == null) {
      return 0;
    }
    return read(b, 0, b.length);
  }
  
  private int locateLF()
  {
    for (int i = bufferpos; i < bufferlen; i++) {
      if (buffer[i] == 10) {
        return i;
      }
    }
    return -1;
  }
  
  public int readLine(CharArrayBuffer charbuffer)
    throws IOException
  {
    Args.notNull(charbuffer, "Char array buffer");
    int noRead = 0;
    boolean retry = true;
    while (retry)
    {
      int i = locateLF();
      if (i != -1)
      {
        if (linebuffer.isEmpty()) {
          return lineFromReadBuffer(charbuffer, i);
        }
        retry = false;
        int len = i + 1 - bufferpos;
        linebuffer.append(buffer, bufferpos, len);
        bufferpos = (i + 1);
      }
      else
      {
        if (hasBufferedData())
        {
          int len = bufferlen - bufferpos;
          linebuffer.append(buffer, bufferpos, len);
          bufferpos = bufferlen;
        }
        noRead = fillBuffer();
        if (noRead == -1) {
          retry = false;
        }
      }
      if ((maxLineLen > 0) && (linebuffer.length() >= maxLineLen)) {
        throw new IOException("Maximum line length limit exceeded");
      }
    }
    if ((noRead == -1) && (linebuffer.isEmpty())) {
      return -1;
    }
    return lineFromLineBuffer(charbuffer);
  }
  
  private int lineFromLineBuffer(CharArrayBuffer charbuffer)
    throws IOException
  {
    int len = linebuffer.length();
    if (len > 0)
    {
      if (linebuffer.byteAt(len - 1) == 10) {
        len--;
      }
      if ((len > 0) && 
        (linebuffer.byteAt(len - 1) == 13)) {
        len--;
      }
    }
    if (ascii)
    {
      charbuffer.append(linebuffer, 0, len);
    }
    else
    {
      ByteBuffer bbuf = ByteBuffer.wrap(linebuffer.buffer(), 0, len);
      len = appendDecoded(charbuffer, bbuf);
    }
    linebuffer.clear();
    return len;
  }
  
  private int lineFromReadBuffer(CharArrayBuffer charbuffer, int position)
    throws IOException
  {
    int off = bufferpos;
    int i = position;
    bufferpos = (i + 1);
    if ((i > off) && (buffer[(i - 1)] == 13)) {
      i--;
    }
    int len = i - off;
    if (ascii)
    {
      charbuffer.append(buffer, off, len);
    }
    else
    {
      ByteBuffer bbuf = ByteBuffer.wrap(buffer, off, len);
      len = appendDecoded(charbuffer, bbuf);
    }
    return len;
  }
  
  private int appendDecoded(CharArrayBuffer charbuffer, ByteBuffer bbuf)
    throws IOException
  {
    if (!bbuf.hasRemaining()) {
      return 0;
    }
    if (decoder == null)
    {
      decoder = charset.newDecoder();
      decoder.onMalformedInput(onMalformedCharAction);
      decoder.onUnmappableCharacter(onUnmappableCharAction);
    }
    if (cbuf == null) {
      cbuf = CharBuffer.allocate(1024);
    }
    decoder.reset();
    int len = 0;
    while (bbuf.hasRemaining())
    {
      CoderResult result = decoder.decode(bbuf, cbuf, true);
      len += handleDecodingResult(result, charbuffer, bbuf);
    }
    CoderResult result = decoder.flush(cbuf);
    len += handleDecodingResult(result, charbuffer, bbuf);
    cbuf.clear();
    return len;
  }
  
  private int handleDecodingResult(CoderResult result, CharArrayBuffer charbuffer, ByteBuffer bbuf)
    throws IOException
  {
    if (result.isError()) {
      result.throwException();
    }
    cbuf.flip();
    int len = cbuf.remaining();
    while (cbuf.hasRemaining()) {
      charbuffer.append(cbuf.get());
    }
    cbuf.compact();
    return len;
  }
  
  public String readLine()
    throws IOException
  {
    CharArrayBuffer charbuffer = new CharArrayBuffer(64);
    int l = readLine(charbuffer);
    if (l != -1) {
      return charbuffer.toString();
    }
    return null;
  }
  
  public HttpTransportMetrics getMetrics()
  {
    return metrics;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.AbstractSessionInputBuffer
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import org.apache.http.Consts;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.BufferInfo;
import org.apache.http.io.HttpTransportMetrics;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.params.HttpParams;
import org.apache.http.util.Args;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.CharArrayBuffer;

@Deprecated
@NotThreadSafe
public abstract class AbstractSessionOutputBuffer
  implements SessionOutputBuffer, BufferInfo
{
  private static final byte[] CRLF = { 13, 10 };
  private OutputStream outstream;
  private ByteArrayBuffer buffer;
  private Charset charset;
  private boolean ascii;
  private int minChunkLimit;
  private HttpTransportMetricsImpl metrics;
  private CodingErrorAction onMalformedCharAction;
  private CodingErrorAction onUnmappableCharAction;
  private CharsetEncoder encoder;
  private ByteBuffer bbuf;
  
  protected AbstractSessionOutputBuffer(OutputStream outstream, int buffersize, Charset charset, int minChunkLimit, CodingErrorAction malformedCharAction, CodingErrorAction unmappableCharAction)
  {
    Args.notNull(outstream, "Input stream");
    Args.notNegative(buffersize, "Buffer size");
    this.outstream = outstream;
    buffer = new ByteArrayBuffer(buffersize);
    this.charset = (charset != null ? charset : Consts.ASCII);
    ascii = this.charset.equals(Consts.ASCII);
    encoder = null;
    this.minChunkLimit = (minChunkLimit >= 0 ? minChunkLimit : 512);
    metrics = createTransportMetrics();
    onMalformedCharAction = (malformedCharAction != null ? malformedCharAction : CodingErrorAction.REPORT);
    
    onUnmappableCharAction = (unmappableCharAction != null ? unmappableCharAction : CodingErrorAction.REPORT);
  }
  
  public AbstractSessionOutputBuffer() {}
  
  protected void init(OutputStream outstream, int buffersize, HttpParams params)
  {
    Args.notNull(outstream, "Input stream");
    Args.notNegative(buffersize, "Buffer size");
    Args.notNull(params, "HTTP parameters");
    this.outstream = outstream;
    buffer = new ByteArrayBuffer(buffersize);
    String charset = (String)params.getParameter("http.protocol.element-charset");
    this.charset = (charset != null ? Charset.forName(charset) : Consts.ASCII);
    ascii = this.charset.equals(Consts.ASCII);
    encoder = null;
    minChunkLimit = params.getIntParameter("http.connection.min-chunk-limit", 512);
    metrics = createTransportMetrics();
    CodingErrorAction a1 = (CodingErrorAction)params.getParameter("http.malformed.input.action");
    
    onMalformedCharAction = (a1 != null ? a1 : CodingErrorAction.REPORT);
    CodingErrorAction a2 = (CodingErrorAction)params.getParameter("http.unmappable.input.action");
    
    onUnmappableCharAction = (a2 != null ? a2 : CodingErrorAction.REPORT);
  }
  
  protected HttpTransportMetricsImpl createTransportMetrics()
  {
    return new HttpTransportMetricsImpl();
  }
  
  public int capacity()
  {
    return buffer.capacity();
  }
  
  public int length()
  {
    return buffer.length();
  }
  
  public int available()
  {
    return capacity() - length();
  }
  
  protected void flushBuffer()
    throws IOException
  {
    int len = buffer.length();
    if (len > 0)
    {
      outstream.write(buffer.buffer(), 0, len);
      buffer.clear();
      metrics.incrementBytesTransferred(len);
    }
  }
  
  public void flush()
    throws IOException
  {
    flushBuffer();
    outstream.flush();
  }
  
  public void write(byte[] b, int off, int len)
    throws IOException
  {
    if (b == null) {
      return;
    }
    if ((len > minChunkLimit) || (len > buffer.capacity()))
    {
      flushBuffer();
      
      outstream.write(b, off, len);
      metrics.incrementBytesTransferred(len);
    }
    else
    {
      int freecapacity = buffer.capacity() - buffer.length();
      if (len > freecapacity) {
        flushBuffer();
      }
      buffer.append(b, off, len);
    }
  }
  
  public void write(byte[] b)
    throws IOException
  {
    if (b == null) {
      return;
    }
    write(b, 0, b.length);
  }
  
  public void write(int b)
    throws IOException
  {
    if (buffer.isFull()) {
      flushBuffer();
    }
    buffer.append(b);
  }
  
  public void writeLine(String s)
    throws IOException
  {
    if (s == null) {
      return;
    }
    if (s.length() > 0) {
      if (ascii)
      {
        for (int i = 0; i < s.length(); i++) {
          write(s.charAt(i));
        }
      }
      else
      {
        CharBuffer cbuf = CharBuffer.wrap(s);
        writeEncoded(cbuf);
      }
    }
    write(CRLF);
  }
  
  public void writeLine(CharArrayBuffer charbuffer)
    throws IOException
  {
    if (charbuffer == null) {
      return;
    }
    if (ascii)
    {
      int off = 0;
      int remaining = charbuffer.length();
      while (remaining > 0)
      {
        int chunk = buffer.capacity() - buffer.length();
        chunk = Math.min(chunk, remaining);
        if (chunk > 0) {
          buffer.append(charbuffer, off, chunk);
        }
        if (buffer.isFull()) {
          flushBuffer();
        }
        off += chunk;
        remaining -= chunk;
      }
    }
    else
    {
      CharBuffer cbuf = CharBuffer.wrap(charbuffer.buffer(), 0, charbuffer.length());
      writeEncoded(cbuf);
    }
    write(CRLF);
  }
  
  private void writeEncoded(CharBuffer cbuf)
    throws IOException
  {
    if (!cbuf.hasRemaining()) {
      return;
    }
    if (encoder == null)
    {
      encoder = charset.newEncoder();
      encoder.onMalformedInput(onMalformedCharAction);
      encoder.onUnmappableCharacter(onUnmappableCharAction);
    }
    if (bbuf == null) {
      bbuf = ByteBuffer.allocate(1024);
    }
    encoder.reset();
    while (cbuf.hasRemaining())
    {
      CoderResult result = encoder.encode(cbuf, bbuf, true);
      handleEncodingResult(result);
    }
    CoderResult result = encoder.flush(bbuf);
    handleEncodingResult(result);
    bbuf.clear();
  }
  
  private void handleEncodingResult(CoderResult result)
    throws IOException
  {
    if (result.isError()) {
      result.throwException();
    }
    bbuf.flip();
    while (bbuf.hasRemaining()) {
      write(bbuf.get());
    }
    bbuf.compact();
  }
  
  public HttpTransportMetrics getMetrics()
  {
    return metrics;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.AbstractSessionOutputBuffer
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.MalformedChunkCodingException;
import org.apache.http.TruncatedChunkException;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.BufferInfo;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;

@NotThreadSafe
public class ChunkedInputStream
  extends InputStream
{
  private static final int CHUNK_LEN = 1;
  private static final int CHUNK_DATA = 2;
  private static final int CHUNK_CRLF = 3;
  private static final int BUFFER_SIZE = 2048;
  private final SessionInputBuffer in;
  private final CharArrayBuffer buffer;
  private int state;
  private int chunkSize;
  private int pos;
  private boolean eof = false;
  private boolean closed = false;
  private Header[] footers = new Header[0];
  
  public ChunkedInputStream(SessionInputBuffer in)
  {
    this.in = ((SessionInputBuffer)Args.notNull(in, "Session input buffer"));
    pos = 0;
    buffer = new CharArrayBuffer(16);
    state = 1;
  }
  
  public int available()
    throws IOException
  {
    if ((in instanceof BufferInfo))
    {
      int len = ((BufferInfo)in).length();
      return Math.min(len, chunkSize - pos);
    }
    return 0;
  }
  
  public int read()
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted read from closed stream.");
    }
    if (eof) {
      return -1;
    }
    if (state != 2)
    {
      nextChunk();
      if (eof) {
        return -1;
      }
    }
    int b = in.read();
    if (b != -1)
    {
      pos += 1;
      if (pos >= chunkSize) {
        state = 3;
      }
    }
    return b;
  }
  
  public int read(byte[] b, int off, int len)
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted read from closed stream.");
    }
    if (eof) {
      return -1;
    }
    if (state != 2)
    {
      nextChunk();
      if (eof) {
        return -1;
      }
    }
    int bytesRead = in.read(b, off, Math.min(len, chunkSize - pos));
    if (bytesRead != -1)
    {
      pos += bytesRead;
      if (pos >= chunkSize) {
        state = 3;
      }
      return bytesRead;
    }
    eof = true;
    throw new TruncatedChunkException("Truncated chunk ( expected size: " + chunkSize + "; actual size: " + pos + ")");
  }
  
  public int read(byte[] b)
    throws IOException
  {
    return read(b, 0, b.length);
  }
  
  private void nextChunk()
    throws IOException
  {
    chunkSize = getChunkSize();
    if (chunkSize < 0) {
      throw new MalformedChunkCodingException("Negative chunk size");
    }
    state = 2;
    pos = 0;
    if (chunkSize == 0)
    {
      eof = true;
      parseTrailerHeaders();
    }
  }
  
  private int getChunkSize()
    throws IOException
  {
    int st = state;
    switch (st)
    {
    case 3: 
      buffer.clear();
      int bytesRead1 = in.readLine(buffer);
      if (bytesRead1 == -1) {
        return 0;
      }
      if (!buffer.isEmpty()) {
        throw new MalformedChunkCodingException("Unexpected content at the end of chunk");
      }
      state = 1;
    case 1: 
      buffer.clear();
      int bytesRead2 = in.readLine(buffer);
      if (bytesRead2 == -1) {
        return 0;
      }
      int separator = buffer.indexOf(59);
      if (separator < 0) {
        separator = buffer.length();
      }
      try
      {
        return Integer.parseInt(buffer.substringTrimmed(0, separator), 16);
      }
      catch (NumberFormatException e)
      {
        throw new MalformedChunkCodingException("Bad chunk header");
      }
    }
    throw new IllegalStateException("Inconsistent codec state");
  }
  
  private void parseTrailerHeaders()
    throws IOException
  {
    try
    {
      footers = AbstractMessageParser.parseHeaders(in, -1, -1, null);
    }
    catch (HttpException ex)
    {
      IOException ioe = new MalformedChunkCodingException("Invalid footer: " + ex.getMessage());
      
      ioe.initCause(ex);
      throw ioe;
    }
  }
  
  public void close()
    throws IOException
  {
    if (!closed) {
      try
      {
        if (!eof)
        {
          byte[] buffer = new byte['?'];
          while (read(buffer) >= 0) {}
        }
      }
      finally
      {
        eof = true;
        closed = true;
      }
    }
  }
  
  public Header[] getFooters()
  {
    return (Header[])footers.clone();
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.ChunkedInputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.SessionOutputBuffer;

@NotThreadSafe
public class ChunkedOutputStream
  extends OutputStream
{
  private final SessionOutputBuffer out;
  private final byte[] cache;
  private int cachePosition = 0;
  private boolean wroteLastChunk = false;
  private boolean closed = false;
  
  @Deprecated
  public ChunkedOutputStream(SessionOutputBuffer out, int bufferSize)
    throws IOException
  {
    this(bufferSize, out);
  }
  
  @Deprecated
  public ChunkedOutputStream(SessionOutputBuffer out)
    throws IOException
  {
    this(2048, out);
  }
  
  public ChunkedOutputStream(int bufferSize, SessionOutputBuffer out)
  {
    cache = new byte[bufferSize];
    this.out = out;
  }
  
  protected void flushCache()
    throws IOException
  {
    if (cachePosition > 0)
    {
      out.writeLine(Integer.toHexString(cachePosition));
      out.write(cache, 0, cachePosition);
      out.writeLine("");
      cachePosition = 0;
    }
  }
  
  protected void flushCacheWithAppend(byte[] bufferToAppend, int off, int len)
    throws IOException
  {
    out.writeLine(Integer.toHexString(cachePosition + len));
    out.write(cache, 0, cachePosition);
    out.write(bufferToAppend, off, len);
    out.writeLine("");
    cachePosition = 0;
  }
  
  protected void writeClosingChunk()
    throws IOException
  {
    out.writeLine("0");
    out.writeLine("");
  }
  
  public void finish()
    throws IOException
  {
    if (!wroteLastChunk)
    {
      flushCache();
      writeClosingChunk();
      wroteLastChunk = true;
    }
  }
  
  public void write(int b)
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted write to closed stream.");
    }
    cache[cachePosition] = ((byte)b);
    cachePosition += 1;
    if (cachePosition == cache.length) {
      flushCache();
    }
  }
  
  public void write(byte[] b)
    throws IOException
  {
    write(b, 0, b.length);
  }
  
  public void write(byte[] src, int off, int len)
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted write to closed stream.");
    }
    if (len >= cache.length - cachePosition)
    {
      flushCacheWithAppend(src, off, len);
    }
    else
    {
      System.arraycopy(src, off, cache, cachePosition, len);
      cachePosition += len;
    }
  }
  
  public void flush()
    throws IOException
  {
    flushCache();
    out.flush();
  }
  
  public void close()
    throws IOException
  {
    if (!closed)
    {
      closed = true;
      finish();
      out.flush();
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.ChunkedOutputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.ConnectionClosedException;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.BufferInfo;
import org.apache.http.io.SessionInputBuffer;
import org.apache.http.util.Args;

@NotThreadSafe
public class ContentLengthInputStream
  extends InputStream
{
  private static final int BUFFER_SIZE = 2048;
  private final long contentLength;
  private long pos = 0L;
  private boolean closed = false;
  private SessionInputBuffer in = null;
  
  public ContentLengthInputStream(SessionInputBuffer in, long contentLength)
  {
    this.in = ((SessionInputBuffer)Args.notNull(in, "Session input buffer"));
    this.contentLength = Args.notNegative(contentLength, "Content length");
  }
  
  public void close()
    throws IOException
  {
    if (!closed) {
      try
      {
        if (pos < contentLength)
        {
          byte[] buffer = new byte['?'];
          while (read(buffer) >= 0) {}
        }
      }
      finally
      {
        closed = true;
      }
    }
  }
  
  public int available()
    throws IOException
  {
    if ((in instanceof BufferInfo))
    {
      int len = ((BufferInfo)in).length();
      return Math.min(len, (int)(contentLength - pos));
    }
    return 0;
  }
  
  public int read()
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted read from closed stream.");
    }
    if (pos >= contentLength) {
      return -1;
    }
    int b = in.read();
    if (b == -1)
    {
      if (pos < contentLength) {
        throw new ConnectionClosedException("Premature end of Content-Length delimited message body (expected: " + contentLength + "; received: " + pos);
      }
    }
    else {
      pos += 1L;
    }
    return b;
  }
  
  public int read(byte[] b, int off, int len)
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted read from closed stream.");
    }
    if (pos >= contentLength) {
      return -1;
    }
    int chunk = len;
    if (pos + len > contentLength) {
      chunk = (int)(contentLength - pos);
    }
    int count = in.read(b, off, chunk);
    if ((count == -1) && (pos < contentLength)) {
      throw new ConnectionClosedException("Premature end of Content-Length delimited message body (expected: " + contentLength + "; received: " + pos);
    }
    if (count > 0) {
      pos += count;
    }
    return count;
  }
  
  public int read(byte[] b)
    throws IOException
  {
    return read(b, 0, b.length);
  }
  
  public long skip(long n)
    throws IOException
  {
    if (n <= 0L) {
      return 0L;
    }
    byte[] buffer = new byte['?'];
    
    long remaining = Math.min(n, contentLength - pos);
    
    long count = 0L;
    while (remaining > 0L)
    {
      int l = read(buffer, 0, (int)Math.min(2048L, remaining));
      if (l == -1) {
        break;
      }
      count += l;
      remaining -= l;
    }
    return count;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.io.ContentLengthInputStream
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.io;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.util.Args;

@NotThreadSafe
public class ContentLengthOutputStream
  extends OutputStream
{
  private final SessionOutputBuffer out;
  private final long contentLength;
  private long total = 0L;
  private boolean closed = false;
  
  public ContentLengthOutputStream(SessionOutputBuffer out, long contentLength)
  {
    this.out = ((SessionOutputBuffer)Args.notNull(out, "Session output buffer"));
    this.contentLength = Args.notNegative(contentLength, "Content length");
  }
  
  public void close()
    throws IOException
  {
    if (!closed)
    {
      closed = true;
      out.flush();
    }
  }
  
  public void flush()
    throws IOException
  {
    out.flush();
  }
  
  public void write(byte[] b, int off, int len)
    throws IOException
  {
    if (closed) {
      throw new IOException("Attempted write to closed stream.");
    }
    if (total < contentLength)
    {
      long max = contentLength - total;
      int chunk = len;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd