code: public final static int convertToUTF8Bytes(int unicodeScalar, byte[] outputBytes, int offset, int maxLength)
      {
          int dst = offset;
          int end = offset + maxLength;
  
          switch (getUTF8ByteCount(unicodeScalar)) {
          case 1:
              if (dst >= end) throw new ArrayIndexOutOfBoundsException();
              outputBytes[dst++] = (byte)(unicodeScalar & 0xff);
              break;
          case 2:
              if (dst+1 >= end) throw new ArrayIndexOutOfBoundsException();
              outputBytes[dst++] = getByte1Of2(unicodeScalar);
              outputBytes[dst++] = getByte2Of2(unicodeScalar);
              break;
          case 3:
              if (dst+2 >= end) throw new ArrayIndexOutOfBoundsException();
              outputBytes[dst++] = getByte1Of3(unicodeScalar);
              outputBytes[dst++] = getByte2Of3(unicodeScalar);
              outputBytes[dst++] = getByte3Of3(unicodeScalar);
              break;
          case 4:
              if (dst+3 >= end) throw new ArrayIndexOutOfBoundsException();
              outputBytes[dst++] = getByte1Of4(unicodeScalar);
              outputBytes[dst++] = getByte2Of4(unicodeScalar);
              outputBytes[dst++] = getByte3Of4(unicodeScalar);
              outputBytes[dst++] = getByte4Of4(unicodeScalar);
              break;
          }
          return dst - offset;
      }
docs: this helper converts the unicodeScalar to a sequence of utf8 bytes
  and copies those bytes into the supplied outputBytes array.  If there
  is insufficient room in the array to hold the generated bytes it will
  throw an ArrayIndexOutOfBoundsException.  It does not check for the
  validity of the passed in unicodeScalar thoroughly, however it will
  throw an InvalidUnicodeCodePoint if the value is less than negative
  or the UTF8 encoding would exceed 4 bytes.
  @param unicodeScalar scalar to convert
  @param outputBytes user output array to fill with UTF8 bytes
  @param offset first array element to fill
  @param maxLength maximum number of array elements to fill
  @return number of bytes written to the output array