public byte[] readRawBytes(final int size) throws IOException {
  if (size < 0) {
    throw InvalidProtocolBufferException.negativeSize();
  }
  if (totalBytesRetired + bufferPos + size > currentLimit) {
    skipRawBytes(currentLimit - totalBytesRetired - bufferPos);
    throw InvalidProtocolBufferException.truncatedMessage();
  }
  if (size <= bufferSize - bufferPos) {
    final byte[] bytes=new byte[size];
    System.arraycopy(buffer,bufferPos,bytes,0,size);
    bufferPos+=size;
    return bytes;
  }
 else   if (size < BUFFER_SIZE) {
    final byte[] bytes=new byte[size];
    int pos=bufferSize - bufferPos;
    System.arraycopy(buffer,bufferPos,bytes,0,pos);
    bufferPos=bufferSize;
    refillBuffer(true);
    while (size - pos > bufferSize) {
      System.arraycopy(buffer,0,bytes,pos,bufferSize);
      pos+=bufferSize;
      bufferPos=bufferSize;
      refillBuffer(true);
    }
    System.arraycopy(buffer,0,bytes,pos,size - pos);
    bufferPos=size - pos;
    return bytes;
  }
 else {
    final int originalBufferPos=bufferPos;
    final int originalBufferSize=bufferSize;
    totalBytesRetired+=bufferSize;
    bufferPos=0;
    bufferSize=0;
    int sizeLeft=size - (originalBufferSize - originalBufferPos);
    final List<byte[]> chunks=new ArrayList<byte[]>();
    while (sizeLeft > 0) {
      final byte[] chunk=new byte[Math.min(sizeLeft,BUFFER_SIZE)];
      int pos=0;
      while (pos < chunk.length) {
        final int n=(input == null) ? -1 : input.read(chunk,pos,chunk.length - pos);
        if (n == -1) {
          throw InvalidProtocolBufferException.truncatedMessage();
        }
        totalBytesRetired+=n;
        pos+=n;
      }
      sizeLeft-=chunk.length;
      chunks.add(chunk);
    }
    final byte[] bytes=new byte[size];
    int pos=originalBufferSize - originalBufferPos;
    System.arraycopy(buffer,originalBufferPos,bytes,0,pos);
    for (    final byte[] chunk : chunks) {
      System.arraycopy(chunk,0,bytes,pos,chunk.length);
      pos+=chunk.length;
    }
    return bytes;
  }
}

public byte [ ] readRawBytes ( final int size ) throws IOException { if ( size < 0 ) { throw InvalidProtocolBufferException . negativeSize ( ) ; } if ( totalBytesRetired + bufferPos + size > currentLimit ) { skipRawBytes ( currentLimit - totalBytesRetired - bufferPos ) ; throw InvalidProtocolBufferException . truncatedMessage ( ) ; } if ( size <= bufferSize - bufferPos ) { final byte [ ] bytes = new byte [ size ] ; System . arraycopy ( buffer , bufferPos , bytes , 0 , size ) ; bufferPos += size ; return bytes ; } else if ( size < BUFFER_SIZE ) { final byte [ ] bytes = new byte [ size ] ; int pos = bufferSize - bufferPos ; System . arraycopy ( buffer , bufferPos , bytes , 0 , pos ) ; bufferPos = bufferSize ; refillBuffer ( true ) ; while ( size - pos > bufferSize ) { System . arraycopy ( buffer , 0 , bytes , pos , bufferSize ) ; pos += bufferSize ; bufferPos = bufferSize ; refillBuffer ( true ) ; } System . arraycopy ( buffer , 0 , bytes , pos , size - pos ) ; bufferPos = size - pos ; return bytes ; } else { final int originalBufferPos = bufferPos ; final int originalBufferSize = bufferSize ; totalBytesRetired += bufferSize ; bufferPos = 0 ; bufferSize = 0 ; int sizeLeft = size - ( originalBufferSize - originalBufferPos ) ; final List < byte [ ] > chunks = new ArrayList < byte [ ] > ( ) ; while ( sizeLeft > 0 ) { final byte [ ] chunk = new byte [ Math . min ( sizeLeft , BUFFER_SIZE ) ] ; int pos = 0 ; while ( pos < chunk . length ) { final int n = ( input == null ) ? - 1 : input . read ( chunk , pos , chunk . length - pos ) ; if ( n == - 1 ) { throw InvalidProtocolBufferException . truncatedMessage ( ) ; } totalBytesRetired += n ; pos += n ; } sizeLeft -= chunk . length ; chunks . add ( chunk ) ; } final byte [ ] bytes = new byte [ size ] ; int pos = originalBufferSize - originalBufferPos ; System . arraycopy ( buffer , originalBufferPos , bytes , 0 , pos ) ; for ( final byte [ ] chunk : chunks ) { System . arraycopy ( chunk , 0 , bytes , pos , chunk . length ) ; pos += chunk . length ; } return bytes ; } }
read a fixed size of bytes from the input .
