📄 pduutils.java
字号:
}
else
{
sb.append(", TP-MMS: (has no messages)");
}
}
//TP-RD
if (hasTpField(pdu, "TpRd") != null)
{
if (hasTpField(pdu, "TpRd"))
{
sb.append(", TP-RD: (Reject duplicates)");
}
else
{
sb.append(", TP-RD: (allow duplicates)");
}
}
//TP-VPF
if ((hasTpField(pdu, "TpVpf") != null) && (hasTpField(pdu, "TpVpf") != false))
{
switch (getTpField(pdu, "TpVpf"))
{
case PduUtils.TP_VPF_INTEGER:
sb.append(", TP-VPF: (validity format, integer");
break;
case PduUtils.TP_VPF_TIMESTAMP:
sb.append(", TP-VPF: (validity format, timestamp");
break;
case PduUtils.TP_VPF_NONE:
sb.append(", TP-VPF: (validity format, none)");
break;
}
}
// TP-SRI
if (hasTpField(pdu, "TpSri") != null)
{
if (hasTpField(pdu, "TpSri"))
{
sb.append(", TP-SRI: (Requests Status Report)");
}
else
{
sb.append(", TP-SRI: (No Status Report)");
}
}
// TP-SRR
if (hasTpField(pdu, "TpSrr") != null)
{
if (hasTpField(pdu, "TpSrr"))
{
sb.append(", TP-SRR: (Requests Status Report)");
}
else
{
sb.append(", TP-SRR: (No Status Report)");
}
}
//TP-UDHI
if (pdu.hasTpUdhi())
{
sb.append(", TP-UDHI: (has UDH)");
}
else
{
sb.append(", TP-UDHI: (no UDH)");
}
sb.append("]");
sb.append("\n");
return sb.toString();
}
public static String decodeDataCodingScheme(Pdu pdu)
{
StringBuffer sb = new StringBuffer();
switch (PduUtils.extractDcsEncoding(pdu.getDataCodingScheme()))
{
case PduUtils.DCS_ENCODING_7BIT:
sb.append("7-bit GSM Alphabet");
break;
case PduUtils.DCS_ENCODING_8BIT:
sb.append("8-bit encoding");
break;
case PduUtils.DCS_ENCODING_UCS2:
sb.append("UCS2 encoding");
break;
}
// are flash messages are only applicable to general coding group?
if ((pdu.getDataCodingScheme() & ~PduUtils.DCS_CODING_GROUP_GENERAL) == 0)
{
if (PduUtils.extractDcsFlash(pdu.getDataCodingScheme()) == PduUtils.DCS_MESSAGE_CLASS_FLASH)
{
sb.append(", (Flash Message)");
}
}
return sb.toString();
}
public static byte[] encode8bitUserData(String text)
{
try
{
return text.getBytes("ISO8859_1");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static byte[] encodeUcs2UserData(String text)
{
try
{
// UTF-16 Big-Endian, no Byte Order Marker at start
return text.getBytes("UTF-16BE");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static byte[] encode7bitUserData(byte[] udhOctets, byte[] textSeptets)
{
// UDH octets and text have to be encoded together in a single pass
// UDH octets will need to be converted to unencoded septets in order
// to properly pad the data
if (udhOctets == null)
{
// convert string to uncompressed septets
return unencodedSeptetsToEncodedSeptets(textSeptets);
}
else
{
// convert UDH octets as if they were encoded septets
// NOTE: DO NOT DISCARD THE LAST SEPTET IF IT IS ZERO
byte[] udhSeptets = PduUtils.encodedSeptetsToUnencodedSeptets(udhOctets, false);
// combine the two arrays and encode them as a whole
byte[] combined = new byte[udhSeptets.length + textSeptets.length];
System.arraycopy(udhSeptets, 0, combined, 0, udhSeptets.length);
System.arraycopy(textSeptets, 0, combined, udhSeptets.length, textSeptets.length);
// convert encoded byte[] to a PDU string
return unencodedSeptetsToEncodedSeptets(combined);
}
}
public static String decode7bitEncoding(byte[] encodedPduData)
{
return decode7bitEncoding(0, encodedPduData);
}
public static String decode7bitEncoding(int udhLength, byte[] encodedPduData)
{
if (udhLength == 0)
{
// just process the whole pdu as one thing
return unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(encodedPduData));
}
else
{
// need to know how many septets are present in the UDH so they can be removed from the final string
int udhSeptets = getNumSeptetsForOctets(udhLength);
// remove the septets related to the UDH to get the decoded text
return unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(encodedPduData)).substring(udhSeptets);
}
}
public static String decode8bitEncoding(int udhLength, byte[] pduData)
{
// standard 8-bit characters
try
{
return new String(pduData, udhLength, pduData.length - udhLength, "ISO8859_1");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static String decodeUcs2Encoding(int udhLength, byte[] pduData)
{
try
{
// standard unicode
return new String(pduData, udhLength, pduData.length - udhLength, "UTF-16");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static byte swapNibbles(int b)
{
return (byte) (((b << 4) & 0xF0) | ((b >>> 4) & 0x0F));
}
@SuppressWarnings("unused")
public static String readBCDNumbers(int numDigits, byte[] addressData)
{
// reads length BCD numbers from the current position
StringBuffer sb = new StringBuffer();
for (int i = 0; i < addressData.length; i++)
{
int b = addressData[i];
int num1 = b & 0x0F;
sb.append(num1);
int num2 = (b >>> 4) & 0x0F;
if (num2 != 0x0F)
{
// check if fillbits
sb.append(num2);
}
}
return sb.toString();
}
public static int createSwappedBCD(int decimal)
{
// creates a swapped BCD representation of a 2-digit decimal
int tens = (decimal & 0xFF) / 10;
int ones = (decimal & 0xFF) - (tens * 10);
return (ones << 4) | tens;
}
// CONVERSION UTILITIES
// from Java String to Pdu
public static String stringToPdu(String txt)
{
byte[] txtBytes = stringToUnencodedSeptets(txt);
return bytesToPdu(unencodedSeptetsToEncodedSeptets(txtBytes));
}
// from Java String to uncompressed septets (GSM characters)
public static byte[] stringToUnencodedSeptets(String s)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i, j, k, index;
char ch;
k = 0;
for (i = 0; i < s.length(); i++)
{
ch = s.charAt(i);
index = -1;
for (j = 0; j < extAlphabet.length; j++)
if (extAlphabet[j] == ch)
{
index = j;
break;
}
if (index != -1) // An extended char...
{
baos.write((byte) Integer.parseInt(extBytes[index].substring(0, 2), 16));
k++;
baos.write((byte) Integer.parseInt(extBytes[index].substring(2, 4), 16));
k++;
}
else
// Maybe a standard char...
{
index = -1;
for (j = 0; j < stdAlphabet.length; j++)
if (stdAlphabet[j] == ch)
{
index = j;
baos.write((byte) j);
k++;
break;
}
if (index == -1) // Maybe a Greek Char...
{
for (j = 0; j < grcAlphabetRemapping.length; j++)
if (grcAlphabetRemapping[j][0] == ch)
{
index = j;
ch = grcAlphabetRemapping[j][1];
break;
}
if (index != -1)
{
for (j = 0; j < stdAlphabet.length; j++)
if (stdAlphabet[j] == ch)
{
index = j;
baos.write((byte) j);
k++;
break;
}
}
else
// Unknown char replacement...
{
baos.write((byte) ' ');
k++;
}
}
}
}
return baos.toByteArray();
}
// from compress unencoded septets
public static byte[] unencodedSeptetsToEncodedSeptets(byte[] septetBytes)
{
byte[] txtBytes;
byte[] txtSeptets;
int txtBytesLen;
BitSet bits;
int i, j;
txtBytes = septetBytes;
txtBytesLen = txtBytes.length;
bits = new BitSet();
for (i = 0; i < txtBytesLen; i++)
for (j = 0; j < 7; j++)
if ((txtBytes[i] & (1 << j)) != 0) bits.set((i * 7) + j);
// big diff here
int encodedSeptetByteArrayLength = txtBytesLen * 7 / 8 + ((txtBytesLen * 7 % 8 != 0) ? 1 : 0);
txtSeptets = new byte[encodedSeptetByteArrayLength];
for (i = 0; i < encodedSeptetByteArrayLength; i++)
{
for (j = 0; j < 8; j++)
{
txtSeptets[i] |= (byte) ((bits.get((i * 8) + j) ? 1 : 0) << j);
}
}
return txtSeptets;
}
// from GSM characters to java string
public static String unencodedSeptetsToString(byte[] bytes)
{
StringBuffer text;
String extChar;
int i, j;
text = new StringBuffer();
for (i = 0; i < bytes.length; i++)
{
if (bytes[i] == 0x1b)
{
extChar = "1b" + Integer.toHexString(bytes[++i]);
for (j = 0; j < extBytes.length; j++)
if (extBytes[j].equalsIgnoreCase(extChar)) text.append(extAlphabet[j]);
}
else
{
text.append(stdAlphabet[bytes[i]]);
}
}
return text.toString();
}
public static String encodedSeptetsToString(byte[] encodedSeptets)
{
return unencodedSeptetsToString(encodedSeptetsToUnencodedSeptets(encodedSeptets));
}
public static int getNumSeptetsForOctets(int numOctets)
{
return numOctets * 8 / 7 + ((numOctets * 8 % 7 != 0) ? 1 : 0);
}
// decompress encoded septets to unencoded form
public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes)
{
return encodedSeptetsToUnencodedSeptets(octetBytes, true);
}
public static byte[] encodedSeptetsToUnencodedSeptets(byte[] octetBytes, boolean discardLast)
{
byte newBytes[];
BitSet bitSet;
int i, j, value1, value2;
bitSet = new BitSet(octetBytes.length * 8);
value1 = 0;
for (i = 0; i < octetBytes.length; i++)
for (j = 0; j < 8; j++)
{
value1 = (i * 8) + j;
if ((octetBytes[i] & (1 << j)) != 0) bitSet.set(value1);
}
value1++;
// this is a bit count NOT a byte count
value2 = value1 / 7 + ((value1 % 7 != 0) ? 1 : 0); // big diff here
//System.out.println(octetBytes.length);
//System.out.println(value1+" --> "+value2);
if (value2 == 0) value2++;
newBytes = new byte[value2];
for (i = 0; i < value2; i++)
{
for (j = 0; j < 7; j++)
{
if ((value1 + 1) > (i * 7 + j))
{
if (bitSet.get(i * 7 + j))
{
newBytes[i] |= (byte) (1 << j);
}
}
}
}
if (discardLast)
{
// when decoding a 7bit encoded string
// the last septet may become 0, this should be discarded
// since this is an artifact of the encoding not part of the
// original string
// this is only done for decoding 7bit encoded text NOT for
// reversing octets to septets (e.g. for the encoding the UDH)
if (newBytes[newBytes.length - 1] == 0)
{
byte[] retVal = new byte[newBytes.length - 1];
System.arraycopy(newBytes, 0, retVal, 0, retVal.length);
return retVal;
}
}
return newBytes;
}
// converts a PDU style string to a byte array
public static byte[] pduToBytes(String s)
{
byte[] bytes = new byte[s.length() / 2];
for (int i = 0; i < s.length(); i += 2)
{
bytes[i / 2] = (byte) (Integer.parseInt(s.substring(i, i + 2), 16));
}
return bytes;
}
// converts a PDU style string to a bit string
public static String pduToBits(String pduString)
{
return bytesToBits(pduToBytes(pduString));
}
// converts a byte array to PDU style string
public static String bytesToPdu(byte[] bytes)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++)
{
sb.append(byteToPdu(bytes[i] & 0xFF));
}
return sb.toString();
}
// converts a byte array to a bit string
public static String bytesToBits(byte[] b)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++)
{
String bits = Integer.toBinaryString(b[i] & 0xFF);
while (bits.length() < 8)
{
bits = "0" + bits;
}
if (i > 0)
{
sb.append(" ");
}
sb.append(bits);
}
return sb.toString();
}
public static String byteToBits(byte b)
{
String bits = Integer.toBinaryString(b & 0xFF);
while (bits.length() < 8)
{
bits = "0" + bits;
}
return bits;
}
public static String byteToPdu(int b)
{
StringBuffer sb = new StringBuffer();
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1)
{
sb.append("0");
}
sb.append(s);
return sb.toString().toUpperCase();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -