Consume CR + LF

@param buffer
@return the number of bytes we consumed, which should be two
if we indeed consumed CRLF or zero otherwise.
433798
public static int consumeCRLF(final Buffer buffer) throws SipParseException {
        try {
            buffer.markReaderIndex();
            final byte cr = buffer.readByte();
            final byte lf = buffer.readByte();
            if (cr == CR && lf == LF) {
                return 2;
            }
        } catch (final IndexOutOfBoundsException e) {
            // fall through
        } catch (final IOException e) {
            throw new SipParseException(buffer.getReaderIndex(), UNABLE_TO_READ_FROM_STREAM, e);
        }
        buffer.resetReaderIndex();
        return 0;
    }
1126937
public static byte[] readBytes(ByteBuffer buffer) {
		final int remaining = buffer.remaining();
		byte[] ab = new byte[remaining];
		buffer.get(ab);
		return ab;
	}
1244176
private Buffer consumeUntil(final Buffer name) {
        try {
            while (this.params.hasReadableBytes()) {
                SipParser.consumeSEMI(this.params);
                final Buffer[] keyValue = SipParser.consumeGenericParam(this.params);
                ensureParamsMap();
                final Buffer value = keyValue[1] == null ? Buffers.EMPTY_BUFFER : keyValue[1];
                this.paramMap.put(keyValue[0], value);

                if (name != null && name.equals(keyValue[0])) {
                    return value;
                }
            }
            return null;
        } catch (final IndexOutOfBoundsException e) {
            throw new SipParseException(this.params.getReaderIndex(),
                    "Unable to process the value due to a IndexOutOfBoundsException", e);
        } catch (final IOException e) {
            throw new SipParseException(this.params.getReaderIndex(),
                    "Could not read from the underlying stream while parsing the value");
        }

    }
791945
public static int crc16(ByteBuffer bytes) {

        int crc = 0x0000;

        while (bytes.hasRemaining()) {
            crc = doCrc(bytes.get(), crc);
        }

        return crc & 0xFFFF;
    }
1252432
private BigDecimal parseMediumNumber(final NumberBuffer buffer) {
        BigDecimal sum = BigDecimal.ZERO;
        BigDecimal result = parseMediumPair(buffer);

        if (result == null) {
            return null;
        }

        while (result != null) {
            sum = sum.add(result);
            result = parseMediumPair(buffer);
        }

        return sum;
    }
278707
private int extractNextTlvElementLength(IoBuffer in) {
        if (!hasRemainingData(in, 2)) {
            return NOT_ENOUGH_DATA;
        }
        try {
            in.mark();
            int firstByte = in.getUnsigned();
            boolean tlv8 = (firstByte & TLV16_MASK) == 0;
            if (tlv8) {
                // 8 bit length. NB! Reads one unsigned byte as an integer
                return in.getUnsigned() + TLV8_HEADER_LENGTH;
            }
            // skip tlv16 LSB byte
            in.skip(1);
            if (!hasRemainingData(in, 2)) {
                return NOT_ENOUGH_DATA;
            }
            // 16 bit length. NB! Reads two bytes unsigned integer
            return in.getUnsignedShort() + TLV16_HEADER_LENGTH;
        } finally {
            in.reset();
        }
    }
183495
private int fillBuffer() throws IOException {
    int n = super.read(buffer, 0, BUF_SIZE);
    if (n >= 0) {
      real_pos += n;
      buf_end = n;
      buf_pos = 0;
    }
    return n;
  }
651593
public int read(byte []buf, int offset, int length) throws IOException
  {
    int strlen = _length;

    int start = offset;
    int end = offset + length;
    
    int index = _index;
    for (; index < strlen && offset < end; index++) {
      int ch = _string.charAt(index);

      if (ch < 0x80)
        buf[offset++] = (byte) ch;
      else if (ch < 0x800 && offset + 1 < end) {
        buf[offset++] = (byte) (0xc0 | (ch >> 6));
        buf[offset++] = (byte) (0x80 | (ch & 0x3f));
      }
      else if (ch < 0x8000 && offset + 2 < end) {
        buf[offset++] = (byte) (0xe0 | (ch >> 12));
        buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
        buf[offset++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
      }
      else if (offset == start) {
        throw new IllegalStateException("buffer length is not large enough to decode UTF-8 data");
      }
      else {
        break;
      }
    }

    _index = index;

    return start < offset ? offset - start : -1;
  }
279541
public static int takeInt(ByteBuffer buffer) {
        int val = 0;
        boolean started = false;
        boolean minus = false;
        int i;
        for (i = buffer.position(); i < buffer.limit(); i++) {
            byte b = buffer.get(i);
            if (b <= SPACE) {
                if (started)
                    break;
            } else if (b >= '0' && b <= '9') {
                val = val * 10 + (b - '0');
                started = true;
            } else if (b == MINUS && !started) {
                minus = true;
            } else
                break;
        }

        if (started) {
            buffer.position(i);
            return minus ? (-val) : val;
        }
        throw new NumberFormatException(toString(buffer));
    }
869715
private int parseCtrlWord(PushbackInputStream reader) throws IOException {
		int nextChar = 0;
		int result = errOK;
		
		if((nextChar = reader.read()) == -1) {
			return errEndOfFile;
		}
		this.byteCount++;

		StringBuffer parsedCtrlWord = new StringBuffer();
		StringBuffer parsedParam= new StringBuffer();
		RtfCtrlWordData ctrlWordParam = new RtfCtrlWordData();
		
		if(!Character.isLetterOrDigit((char)nextChar)) {
			parsedCtrlWord.append((char)nextChar);
			ctrlWordParam.ctrlWord = parsedCtrlWord.toString();
			result =  this.handleCtrlWord(ctrlWordParam);
			lastCtrlWordParam = ctrlWordParam;
			return result;
		}
		
		do {
			parsedCtrlWord.append((char)nextChar);
			//TODO: catch EOF
			nextChar = reader.read();
			this.byteCount++;
		} while  (Character.isLetter((char)nextChar));
		
		ctrlWordParam.ctrlWord = parsedCtrlWord.toString();

		if(nextChar == '-') {
			ctrlWordParam.isNeg = true;
			if((nextChar = reader.read()) == -1) {
					return errEndOfFile;
			}
			this.byteCount++;
		}
		

		if(Character.isDigit((char)nextChar)) {
			ctrlWordParam.hasParam = true;
			do {
				parsedParam.append((char)nextChar);
				//TODO: catch EOF
				nextChar = reader.read();
				this.byteCount++;
				} while  (Character.isDigit((char)nextChar));
						
			ctrlWordParam.param = parsedParam.toString();
		}
		
		// push this character back into the stream
		if(nextChar != ' ') { 
			reader.unread(nextChar);
		}
		
	    if(debugParser) {
	//	    // debug: insrsid6254399
	//	    if(ctrlWordParam.ctrlWord.equals("proptype") && ctrlWordParam.param.equals("30")) {
	//	    	System.out.print("Debug value found\n");
	//	    }
//		    if(ctrlWordParam.ctrlWord.equals("cf") ) {
//		    	System.out.print("Debug value found\n");
//		    }
	    }
	    
		result = this.handleCtrlWord(ctrlWordParam);
		lastCtrlWordParam = ctrlWordParam;
		return result;

	}
382682
public static long calculateCRC32(byte[] buffer, int offset, int length)
	{
		if (!init_done)
		{
			initialize();
		}
		for (int i = offset; i < offset + length; i++)
		{
			long tmp1 = (crc >>> 8) & 0x00FFFFFFL;
			long tmp2 = values[(int) ((crc ^ Character.toUpperCase((char) buffer[i])) & 0xff)];
			crc = tmp1 ^ tmp2;
			// System.out.println("CRC:   "+crc);
		}
		return crc;
	}
471675
private HttpTokens.Token next(ByteBuffer buffer) {
        byte ch = buffer.get();

        HttpTokens.Token t = HttpTokens.TOKENS[0xff & ch];

        switch (t.getType()) {
            case CNTL:
                throw new IllegalCharacterException(_state, t, buffer);

            case LF:
                _cr = false;
                break;

            case CR:
                if (_cr)
                    throw new BadMessageException("Bad EOL");

                _cr = true;
                if (buffer.hasRemaining()) {
                    // Don't count the CRs and LFs of the chunked encoding.
                    if (_maxHeaderBytes > 0 && (_state == State.HEADER || _state == State.TRAILER))
                        _headerBytes++;
                    return next(buffer);
                }

                return null;

            case ALPHA:
            case DIGIT:
            case TCHAR:
            case VCHAR:
            case HTAB:
            case SPACE:
            case OTEXT:
            case COLON:
                if (_cr)
                    throw new BadMessageException("Bad EOL");
                break;

            default:
                break;
        }

        return t;
    }
134780
private int ensureBuffer(int offset, int length) throws IOException {
		final int lastPos = offset + length - 1;
		final int desiredSize = ((lastPos / BUFFER_SIZE) + 1) * BUFFER_SIZE;
		final int currentSize = this.buffer.length;

		if (desiredSize > currentSize) {
			final byte[] readBuffer = new byte[desiredSize - currentSize];
			final int count = this.in.read(readBuffer);

			if (count > 0) {
				final byte[] newBuffer = new byte[currentSize + count];
				System.arraycopy(this.buffer, 0, newBuffer, 0, currentSize);
				System.arraycopy(readBuffer, 0, newBuffer, currentSize, count);
				this.buffer = newBuffer;
			}
			return (lastPos < this.buffer.length) ? length : length - (lastPos - this.buffer.length + 1);
		}
		return length;
	}
468593
private final void consumeWhiteSpace(){
		char c=cbuf[n];
		while(c==CH_SPACE|| c==CH_LINEFEED || c==CH_TAB || c==CH_CARRIAGE_RETURN){
			n++;
			c=cbuf[n];
		}
	}
301209
def consume_byte
      ib = @buffer.getbyte

      if ib.nil?
        @buffer.reset!

        while ib.nil?
          Fiber.yield
          ib = @buffer.getbyte
        end
      end

      ib
    end
784147
protected TokenCodes findCRLFTokenLength(WsByteBuffer buff) throws MalformedMessageException {

        TokenCodes rc = TokenCodes.TOKEN_RC_MOREDATA;

        if (null == buff) {
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "Null buffer provided");
            }
            return rc;
        }

        // start with any pre-existing data
        int length = this.parsedTokenLength;
        byte b;
        while (true) {
            if (this.bytePosition >= this.byteLimit) {
                if (!fillByteCache(buff)) {
                    // no more data
                    break;
                }
            }
            b = this.byteCache[this.bytePosition++];

            // check for a CRLF
            if (BNFHeaders.CR == b) {
                rc = TokenCodes.TOKEN_RC_DELIM;
                if (HeaderStorage.NOTSET != this.headerChangeLimit) {
                    this.lastCRLFPosition = findCurrentBufferPosition(buff) - 1;
                    this.lastCRLFBufferIndex = this.parseIndex;
                    this.lastCRLFisCR = true;
                }
                break; // out of while
            } else if (BNFHeaders.LF == b) {
                // update counter if linefeed found
                rc = TokenCodes.TOKEN_RC_DELIM;
                this.numCRLFs = 1;
                if (HeaderStorage.NOTSET != this.headerChangeLimit) {
                    this.lastCRLFPosition = findCurrentBufferPosition(buff) - 1;
                    this.lastCRLFBufferIndex = this.parseIndex;
                    this.lastCRLFisCR = false;
                }
                break; // out of while
            }

            length++;
            // check the limit on a token size
            if (length > this.limitTokenSize) {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "findCRLFTokenLength: length is too big: " + length);
                }
                throw new MalformedMessageException("Token length: " + length);
            }
        } // end of the while

        this.parsedTokenLength = length;
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "findCRLFTokenLength returning " + rc.getName() + "; len=" + length);
        }
        return rc;
    }
1128613
protected ByteBuffer getByteBuffer(int bufferLength) {
        if(_buffer == null) {
            _buffer = ByteBuffer.wrap(new byte[bufferLength]);
            _log.info("ByteBuffer allocated for buffering");
        }
        
        return _buffer;
    }