UdhUtil { public static byte[] getMessageWithUdhInBytes(String s, byte dataCoding) { final int udhLength = ((byte)s.charAt(0))+1; final byte[] udh = s.substring(0, udhLength).getBytes(); final byte[] content = DataCodingSpecification.getMessageInBytes(s.substring(udhLength), dataCoding); final byte[] contentWithUdh = new byte[udhLength + content.length]; System.arraycopy(udh, 0, contentWithUdh, 0, udhLength); System.arraycopy(content, 0, contentWithUdh, udhLength, content.length); return contentWithUdh; }  static byte[] getMessageWithUdhInBytes(String s, byte dataCoding);  }
@Test public void testGetMessageWithUdhInBytes() throws Exception { String message = "This is the message"; byte dataCoding = 0; String messageWithUdh = new String(udh).concat(message); byte[] result = UdhUtil.getMessageWithUdhInBytes(messageWithUdh, dataCoding); Assert.assertEquals(udh[0], result[0]); Assert.assertEquals(udh[1], result[1]); Assert.assertEquals(udh[2], result[2]); Assert.assertEquals(udh[3], result[3]); Assert.assertEquals(udh[4], result[4]); Assert.assertEquals(udh[5], result[5]); byte[] content = new byte[result.length-6]; System.arraycopy(result, 6, content, 0, content.length); String decoded = new String(content, "UTF-8"); Assert.assertEquals(message, decoded); }
@Test public void testGetMessageWithUdhInBytes_dataCoding8() throws Exception { String message = "這將是一個長期的短信"; byte dataCoding = 8; String messageWithUdh = new String(udh).concat(message); byte[] result = UdhUtil.getMessageWithUdhInBytes(messageWithUdh, dataCoding); Assert.assertEquals(udh[0], result[0]); Assert.assertEquals(udh[1], result[1]); Assert.assertEquals(udh[2], result[2]); Assert.assertEquals(udh[3], result[3]); Assert.assertEquals(udh[4], result[4]); Assert.assertEquals(udh[5], result[5]); byte[] content = new byte[result.length-6]; System.arraycopy(result, 6, content, 0, content.length); String decoded = new String(content, "UTF-16"); Assert.assertEquals(message, decoded); }