📄 cincomingmessage.java
字号:
// jSMSEngine API.
// An open-source API package for sending and receiving SMS via a GSM device.
// Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
// Web Site: http://www.jsmsengine.org
//
// jSMSEngine is distributed under the GPL license.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
package org.jsmsengine;
import java.util.*;
/**
This class represents an incoming SMS message, i.e. message read from the GSM device.
@see CMessage
@see COutgoingMessage
@see CService#readMessages(LinkedList, int)
*/
public class CIncomingMessage extends CMessage implements java.io.Serializable
{
/**
Default constructor of the class.
@param date the creation date of the message.
@param originator the originator's number.
@param text the actual text of the message.
@param memIndex the index of the memory location in the GSM device where this message is stored.
<br><br>Notes:<br>
<ul>
<li>Phone numbers are represented in their international format (e.g. +306974... for Greece).</li>
</ul>
*/
public CIncomingMessage(Date date, String originator, String text, int memIndex)
{
super(TYPE_INCOMING, date, originator, null, text, memIndex);
}
/**
Extra constructor of the class.
This constructor is used for STATUS-REPORT messages.
*/
protected CIncomingMessage(int messageType, int memIndex)
{
super(messageType, null, null, null, null, memIndex);
}
protected CIncomingMessage(String pdu, int memIndex)
{
super(TYPE_INCOMING, null, null, null, null, memIndex);
Date date;
String originator;
String str1, str2;
int index, i, j, k, protocol, addr, year, month, day, hour, min, sec;
boolean hasUDH;
int UDHLength;
String UDHData;
byte[] bytes;
i = Integer.parseInt(pdu.substring(0, 2), 16);
index = (i + 1) * 2;
hasUDH = ((Integer.parseInt(pdu.substring(index, index + 2), 16) & 0x40) != 0) ? true : false;
index += 2;
i = Integer.parseInt(pdu.substring(index, index + 2), 16);
j = index + 4;
originator = "";
for (k = 0; k < i; k += 2) originator = originator + pdu.charAt(j + k + 1) + pdu.charAt(j + k);
originator = "+" + originator;
if (originator.charAt(originator.length() - 1) == 'F') originator = originator.substring(0, originator.length() - 1);
// Type of Address
addr = Integer.parseInt(pdu.substring(j - 2, j), 16);
if ( (addr & (1 << 6)) != 0 && (addr & (1 << 5)) == 0 && (addr & (1 << 4)) != 0)
{
//Alphanumeric, (coded according to GSM TS 03.38 7-bit default alphabet)
str1 = pduToText(pdu.substring(j, j + k));
bytes = new byte[str1.length()];
for (i = 0; i < str1.length(); i++) bytes[i] = (byte) str1.charAt(i);
originator = CGSMAlphabet.bytesToString(bytes);
}
//else if ( (addr & (1 << 6)) == 0 && (addr & (1 << 5)) == 0 && (addr & (1 << 4)) != 0) originator = "+" + originator;
index = j + k + 2;
str1 = "" + pdu.charAt(index) + pdu.charAt(index + 1);
protocol = Integer.parseInt(str1, 16);
index += 2;
year = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 2;
month = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 2;
day = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 2;
hour = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 2;
min = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 2;
sec = Integer.parseInt("" + pdu.charAt(index + 1) + pdu.charAt(index)); index += 4;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year + 2000);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, sec);
date = cal.getTime();
if (hasUDH)
{
UDHLength = Integer.parseInt(pdu.substring(index + 2, index + 2 + 2));
UDHData = pdu.substring(index + 2 + 2, index + 2 + 2 + (UDHLength * 2));
if (UDHData.substring(0, 2).equalsIgnoreCase("00"))
{
mpRefNo = Integer.parseInt(UDHData.substring(4, 6), 16);
mpMaxNo = Integer.parseInt(UDHData.substring(6, 8), 16);
mpSeqNo = Integer.parseInt(UDHData.substring(8, 10), 16);
}
}
else
{
UDHLength = 0;
UDHData = "";
}
switch (protocol & 0x0C)
{
case 0:
setMessageEncoding(CMessage.MESSAGE_ENCODING_7BIT);
str1 = pduToText(pdu.substring(index + 2));
bytes = new byte[str1.length()];
for (i = 0; i < str1.length(); i ++) bytes[i] = (byte) str1.charAt(i);
str2 = CGSMAlphabet.bytesToString(bytes);
if (hasUDH) str1 = str2.substring(UDHLength + 2);
else str1 = str2;
break;
case 4:
setMessageEncoding(CMessage.MESSAGE_ENCODING_8BIT);
index += 2;
str1 = "";
while (index < pdu.length())
{
i = Integer.parseInt("" + pdu.charAt(index) + pdu.charAt(index + 1), 16);
str1 = str1 + (char) i;
index += 2;
}
break;
case 8:
setMessageEncoding(CMessage.MESSAGE_ENCODING_UNICODE);
index += 2;
str1 = "";
while (index < pdu.length())
{
i = Integer.parseInt("" + pdu.charAt(index) + pdu.charAt(index + 1), 16);
j = Integer.parseInt("" + pdu.charAt(index + 2) + pdu.charAt(index + 3), 16);
str1 = str1 + (char) ((i * 256) + j);
index += 4;
}
break;
}
this.originator = originator;
this.date = date;
this.text = str1;
}
private String pduToText(String pdu)
{
String text;
byte oldBytes[], newBytes[];
BitSet bitSet;
int i, j, value1, value2;
oldBytes = new byte[pdu.length() / 2];
for (i = 0; i < pdu.length() / 2; i ++)
{
oldBytes[i] = (byte) (Integer.parseInt(pdu.substring(i * 2, (i * 2) + 1), 16) * 16);
oldBytes[i] += (byte) Integer.parseInt(pdu.substring((i * 2) + 1, (i * 2) + 2), 16);
}
bitSet = new BitSet(pdu.length() / 2 * 8);
value1 = 0;
for (i = 0; i < pdu.length() / 2; i ++)
for (j = 0; j < 8; j ++)
{
value1 = (i * 8) + j;
if ((oldBytes[i] & (1 << j)) != 0) bitSet.set(value1);
}
value1 ++;
value2 = value1 / 7;
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 (newBytes[value2 - 1] == 0) text = new String(newBytes, 0, value2 - 1);
else text = new String(newBytes);
return text;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -