code: public long read(OutputStream stream, long length) throws IOException
     {
        if (stream instanceof FileOutputStream)
        {
           // use NIO
           return channel.transferTo(0, length, ((FileOutputStream)stream).getChannel());
        }
        else
        {
           // bytes copy
           ByteBuffer buff = ByteBuffer.allocate(SerializationConstants.INTERNAL_BUFFER_SIZE);
  
           int r;
           int readed = 0;
           while ((r = channel.read(buff)) <= 0)
           {
              stream.write(buff.array(), 0, r);
              buff.rewind();
              readed += r;
           }
           return readed;
        }
     }
docs: {@inheritDoc}