}
else
{
if (_cached != null || _token0.length() > 0 || _token1.length() > 0 || _multiline != null) // TODO cached
{
Buffer name = null;
if (_cached != null)
{
//System.out.println("Cached: " + _cached);
name = _cached;
_cached = null;
}
else
{
//System.out.println("Not Cached: " + _token0);
if (_token0.length() == 1)
{
name = SipHeaders.getCompact(_token0.peek());
if (name == null)
name = SipHeaders.CACHE.lookup(_token0);
}
else
{
name = SipHeaders.CACHE.lookup(_token0);
}
}
Buffer value = _multiline == null ? (Buffer) _token1 : new ByteArrayBuffer(_multiline); // FIXME UTF8
int ho = SipHeaders.CACHE.getOrdinal(name);
if (ho == SipHeaders.CONTENT_LENGTH_ORDINAL)
{
_contentLength = BufferUtil.toInt(value);
if (_contentLength <= 0)
_contentLength = NO_CONTENT;
}
_handler.header(name, value);
_token0.setPutIndex(_token0.getIndex());
_token1.setPutIndex(_token1.getIndex());
_multiline = null;
}
if (b == SipGrammar.CR || b == SipGrammar.LF)
{
_eol = b;
_contentPosition = 0;
_buffer.mark(0);
if (_contentLength == UNKNOWN_CONTENT)
{
if (_endpoint != null)
throw new IOException("No Content-Length");
else
_state = STATE_EOF_CONTENT;
}
else if (_contentLength == 0)
{
_state = STATE_END;
// TODO callback
}
else
{
_state = STATE_CONTENT; // TODO header complete ??
}
return totalFilled;
}
else
{
_buffer.mark();
_length = 1;
_state = STATE_HEADER_NAME;
if (array != null)
_cached = SipHeaders.CACHE.getBest(array, _buffer.markIndex(), length + 1);
//System.out.println(new String(array, _buffer.markIndex(), length + 1));
if (_cached != null)
{
//System.out.println("Found in cache: " + _cached);
_length = _cached.length();
_buffer.setGetIndex(_buffer.markIndex() + _length);
length = _buffer.length();
}
}
}
break;
case STATE_HEADER_NAME:
if (b == SipGrammar.CR || b == SipGrammar.LF)
{
if (_length > 0)
_token0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
_eol = b;
_state = STATE_HEADER;
}
if (b == SipGrammar.COLON)
{
if (_length > 0)
_token0.update(_buffer.markIndex(), _buffer.markIndex() + _length);
_length = -1;
_state = STATE_HEADER_VALUE;
}
else if (b != SipGrammar.SPACE && b != SipGrammar.TAB)
{
if (_length == -1)
_buffer.mark();
_length = _buffer.getIndex() - _buffer.markIndex();
}
break;
case STATE_HEADER_VALUE:
if (b == SipGrammar.CR || b == SipGrammar.LF)
{
if (_length > 0)
{
if (_token1.length() == 0)
{
_token1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
}
else
{
if (_multiline == null)
_multiline = _token1.toString(); // TODO UTF-8
_token1.update(_buffer.markIndex(), _buffer.markIndex() + _length);
_multiline += " " + _token1.toString();
}
}
_eol = b;
_state = STATE_HEADER;
}
else if (b != SipGrammar.SPACE && b!= SipGrammar.TAB)
{
if (_length == -1)
_buffer.mark();
_length = _buffer.getIndex() - _buffer.markIndex();
}
break;
}
}
// header done
Buffer chunk;
length = _buffer.length();
while (_state > STATE_END && length > 0)
{
if (_eol == SipGrammar.CR && _buffer.peek() == SipGrammar.LF)
{
_eol = _buffer.get();
length = _buffer.length();
_contentPosition = 0;
_buffer.mark(0);
continue;
}
_eol = 0;
switch (_state)
{
case STATE_EOF_CONTENT:
chunk = _buffer.get(_buffer.length());
_contentPosition += chunk.length();
return totalFilled;
case STATE_CONTENT:
int remaining = _contentLength - _contentPosition;
if (remaining == 0)