public void writeUTF(final String s) throws IOException {
   final int len = UTFUtils.getShortUTFLength(s);
   final int position = this.position;
   final int bufsize = buffer.length;
   int remaining = bufsize - position;
   if (len > bufsize >> 1 || len + 2 > remaining) {
     // the string will take up more than half the buffer or it is bigger than the remaining space
     // so don't bother double-buffering this block
     flush();
     if (len < 253) {
       serialMarshaller.write(TC_BLOCKDATA);
       serialMarshaller.write(len + 2);
       serialMarshaller.writeShort(len);
       UTFUtils.writeUTFBytes(serialMarshaller, s);
     } else {
       serialMarshaller.write(TC_BLOCKDATALONG);
       serialMarshaller.writeInt(len + 2);
       serialMarshaller.writeShort(len);
       UTFUtils.writeUTFBytes(serialMarshaller, s);
     }
   } else {
     // the string will fit in this buffer
     writeShort(len);
     UTFUtils.writeUTFBytes(this, s);
   }
 }
 public void flush() throws IOException {
   final int position = this.position;
   if (position > 256) {
     serialMarshaller.write(TC_BLOCKDATALONG);
     serialMarshaller.writeInt(position);
     serialMarshaller.writeNoBlockFlush(buffer, 0, position);
   } else if (position > 0) {
     serialMarshaller.write(TC_BLOCKDATA);
     serialMarshaller.write(position);
     serialMarshaller.writeNoBlockFlush(buffer, 0, position);
   }
   this.position = 0;
 }
 public void write(final byte[] bytes, final int off, final int len) throws IOException {
   final int bl = buffer.length;
   final int position = this.position;
   if (len > bl - position || len > bl >> 1) {
     flush();
     if (len > 256) {
       serialMarshaller.write(TC_BLOCKDATALONG);
       serialMarshaller.writeInt(len);
       serialMarshaller.write(bytes, off, len);
     } else if (len > 0) {
       serialMarshaller.write(TC_BLOCKDATA);
       serialMarshaller.write(len);
       serialMarshaller.write(bytes, off, len);
     }
   } else {
     System.arraycopy(bytes, off, buffer, position, len);
     this.position = position + len;
   }
 }
 private void doWriteObject(final Object obj, final boolean unshared) throws IOException {
   flush();
   serialMarshaller.doWriteObject(obj, unshared);
   flush();
 }