code: public void writeRawBytes(final ByteString value, int offset, int length)
                              throws IOException {
      if (limit - position >= length) {
        // We have room in the current buffer.
        value.copyTo(buffer, offset, position, length);
        position += length;
      } else {
        // Write extends past current buffer.  Fill the rest of this buffer and
        // flush.
        final int bytesWritten = limit - position;
        value.copyTo(buffer, offset, position, bytesWritten);
        offset += bytesWritten;
        length -= bytesWritten;
        position = limit;
        refreshBuffer();
  
        // Now deal with the rest.
        // Since we have an output stream, this is our buffer
        // and buffer offset == 0
        if (length <= limit) {
          // Fits in new buffer.
          value.copyTo(buffer, offset, 0, length);
          position = length;
        } else {
          // Write is very big, but we can't do it all at once without allocating
          // an a copy of the byte array since ByteString does not give us access
          // to the underlying bytes. Use the InputStream interface on the
          // ByteString and our buffer to copy between the two.
          InputStream inputStreamFrom = value.newInput();
          if (offset != inputStreamFrom.skip(offset)) {
            throw new IllegalStateException("Skip failed? Should never happen.");
          }
          // Use the buffer as the temporary buffer to avoid allocating memory.
          while (length > 0) {
            int bytesToRead = Math.min(length, limit);
            int bytesRead = inputStreamFrom.read(buffer, 0, bytesToRead);
            if (bytesRead != bytesToRead) {
              throw new IllegalStateException("Read failed? Should never happen");
            }
            output.write(buffer, 0, bytesRead);
            length -= bytesRead;
          }
        }
      }
    }
docs: Write part of a byte string.