code: private int writeDataToOutput(EncodedElement data) throws IOException {
  
      int writtenBytes = 0;
      int offset = 0;
      EncodedElement current = data;
      int currentByte = 0;
      byte unfullByte = 0;
      byte[] eleData = null;
      int usableBits = 0;
      int lastByte = 0;
      while(current != null) {
        eleData = current.getData();
        usableBits = current.getUsableBits();
        currentByte = 0;
        //if offset is not zero, merge first byte with existing byte
        if(offset != 0) {
          unfullByte = (byte)(unfullByte | eleData[currentByte++]);
          out.write(unfullByte);
        }
        //write all full bytes of element.
        lastByte = usableBits/8;
        if(lastByte > 0)
          out.write(eleData, currentByte, lastByte-currentByte);
        //save non-full byte(if present), and set "offset" for next element.
        offset = usableBits %8;
        if(offset != 0) {
          unfullByte = eleData[lastByte];
        }
        //update current.
        current = current.getNext();
      }
      //if non-full byte remains. write.
      if(offset != 0) {
        out.write(eleData, lastByte, 1);
      }
      return writtenBytes;
    }
docs: Write the data stored in an EncodedElement to the output stream.
  All data will be written along byte boundaries, but the elements in the
  given list need not end on byte boundaries. If the data of an element
  does not end on a byte boundary, then the space remaining in that last
  byte will be used as an offset, and merged(using an "OR"), with the first
  byte of the following element.
  
  @param data
  @return
  @throws IOException