⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message.java

📁 基于java开发的一套短信应用框架
💻 JAVA
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib;

import java.util.*;

/**
 * The parent of all message-related classes. Most of common fields and
 * attributes of both inbound and outbound messages are placed in this class.
 */
public class Message
{
	private String gtwId;

	private MessageTypes type;

	private Date date;

	private String id;

	private String text;

	private String encodedText;

	private MessageEncodings encoding;

	protected int pid;

	protected int dcs;

	public Message(MessageTypes type, Date date, String text)
	{
		this.gtwId = "";
		this.type = type;
		this.id = "";
		setDate(date);
		this.text = text;
		this.encoding = MessageEncodings.ENC7BIT;
	}

	/**
	 * Returns the creation date. For outbound messages, this is the object's
	 * creation date. For inbound messages, this is the date when the originator
	 * has sent the message.
	 * 
	 * @return the creation date.
	 * @see #setDate(Date)
	 */
	public Date getDate()
	{
		return date == null ? null : new java.util.Date(date.getTime());
	}

	/**
	 * Sets the creation date to a specific date.
	 * 
	 * @param date
	 *            A custom date.
	 * @see #getDate()
	 */
	public void setDate(Date date)
	{
		this.date = (date != null ? new java.util.Date(date.getTime()) : null);
	}

	/**
	 * Returns the message's text encoded using the specified encoding.
	 * 
	 * @return The encoded message text.
	 */
	public String getEncodedText()
	{
		return encodedText;
	}

	void setEncodedText(String encodedText)
	{
		this.encodedText = encodedText;
	}

	/**
	 * Returns the message encoding.
	 * 
	 * @return The message encoding.
	 * @see #setEncoding(MessageEncodings)
	 * @see MessageEncodings
	 */
	public MessageEncodings getEncoding()
	{
		return encoding;
	}

	/**
	 * Sets the message encoding to the specified one.
	 * 
	 * @param encoding
	 *            The message encoding.
	 * @see #getEncoding()
	 * @see MessageEncodings
	 */
	public void setEncoding(MessageEncodings encoding)
	{
		this.encoding = encoding;
		if ((text == null) || (text.length() == 0)) return;
		encodedText = "";
		if (this.encoding == MessageEncodings.ENC7BIT) encodedText = GSMAlphabet.textToPDU(text);
		else if (this.encoding == MessageEncodings.ENC8BIT)
		{
			byte[] bytes = text.getBytes();
			for (int i = 0; i < bytes.length; i++)
			{
				String s = "0" + Integer.toHexString(bytes[i]);
				int from = s.length() - 2;
				encodedText += s.substring(from);
			}
		}
		else if (this.encoding == MessageEncodings.ENCUCS2)
		{
			for (int i = 0; i < text.length(); i++)
			{
				char c = text.charAt(i);
				int high = (int) (c / 256);
				int low = c % 256;
				encodedText += ((Integer.toHexString(high).length() < 2) ? "0" + Integer.toHexString(high) : Integer.toHexString(high));
				encodedText += ((Integer.toHexString(low).length() < 2) ? "0" + Integer.toHexString(low) : Integer.toHexString(low));
			}
		}
		else if (this.encoding == MessageEncodings.ENCCUSTOM)
		{
			if ((dcs & 0x04) == 0) encodedText = GSMAlphabet.textToPDU(text);
			else
			{
				for (int i = 0; i < text.length(); i++)
				{
					char c = text.charAt(i);
					encodedText += ((Integer.toHexString(c).length() < 2) ? "0" + Integer.toHexString(c) : Integer.toHexString(c));
				}
			}
		}
	}

	/**
	 * Returns the ID of the gateway which the message was received from (for
	 * inbound messages) or the message was dispatched from (outbound messages).
	 * 
	 * @return The Gateway ID.
	 * @see #setGatewayId(String)
	 */
	public String getGatewayId()
	{
		return gtwId;
	}

	/**
	 * Sets the message's Gateway ID to a specific value.
	 * 
	 * @param gtwId
	 *            The Gateway ID.
	 * @see #getGatewayId()
	 */
	public void setGatewayId(String gtwId)
	{
		this.gtwId = gtwId;
	}

	/**
	 * Returns the message ID. This field can be used for your own purposes.
	 * 
	 * @return The message ID.
	 * @see #setId(String)
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * Sets the message ID to a specific value.
	 * 
	 * @param id
	 *            The new message ID.
	 * @see #getId()
	 */
	public void setId(String id)
	{
		this.id = id;
	}

	/**
	 * Returns the message text.
	 * 
	 * @return The message text.
	 * @see #setText(String)
	 */
	public String getText()
	{
		return text;
	}

	/**
	 * Sets the message text.
	 * 
	 * @param text
	 *            The message text.
	 * @see #getText()
	 */
	protected void setText(String text)
	{
		this.text = text;
	}

	void addText(String text)
	{
		this.text += text;
	}

	/**
	 * Returns the message type.
	 * 
	 * @return The message type.
	 * @see MessageTypes
	 * @see #setType(MessageTypes)
	 */
	public MessageTypes getType()
	{
		return type;
	}

	void setType(MessageTypes type)
	{
		this.type = type;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -