1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.sms;
36
37 import java.io.*;
38
39 /***
40 * Represents an SMS pdu
41 * <p>
42 * A SMS pdu consists of a user data header (UDH) and the actual content often
43 * called user data (UD).
44 *
45 * @author Markus Eriksson
46 * @version $Id: SmsPdu.java,v 1.21 2005/11/26 14:42:10 c95men Exp $
47 */
48
49 public class SmsPdu
50 {
51 protected SmsUdhElement[] myUdhElements;
52 protected SmsUserData myUd;
53
54 /***
55 * Creates an empty SMS pdu object
56 */
57 public SmsPdu()
58 {
59
60 }
61
62 /***
63 * Creates an SMS pdu object.
64 *
65 * @param theUdhIeis
66 * The UDH elements
67 * @param theUd
68 * The content
69 * @param theUdLength
70 * The length of the content. Can be in octets or septets
71 * depending on the DCS
72 */
73 public SmsPdu(SmsUdhElement[] theUdhIeis, byte[] theUd, int theUdLength, SmsDcs theDataCodingScheme)
74 {
75 setUserDataHeaders(theUdhIeis);
76 setUserData(theUd, theUdLength, theDataCodingScheme);
77 }
78
79 /***
80 * Creates an SMS pdu object.
81 *
82 * @param theUdhIeis
83 * The UDH elements
84 * @param theUd
85 * The content
86 */
87 public SmsPdu(SmsUdhElement[] theUdhIeis, SmsUserData theUd)
88 {
89 setUserDataHeaders(theUdhIeis);
90 setUserData(theUd);
91 }
92
93 /***
94 * Sets the UDH field
95 *
96 * @param theUdhElements
97 * The UDH elements
98 */
99 public void setUserDataHeaders(SmsUdhElement[] theUdhElements)
100 {
101 if (theUdhElements != null)
102 {
103 myUdhElements = new SmsUdhElement[theUdhElements.length];
104
105 System.arraycopy(theUdhElements, 0, myUdhElements, 0, theUdhElements.length);
106 }
107 else
108 {
109 myUdhElements = null;
110 }
111 }
112
113 /***
114 * Returns the user data headers
115 *
116 * @return A byte array representing the UDH fields or null if there aren't
117 * any UDH
118 */
119 public byte[] getUserDataHeaders()
120 {
121 if (myUdhElements == null)
122 {
123 return null;
124 }
125
126 ByteArrayOutputStream baos = new ByteArrayOutputStream(100);
127
128 baos.write((byte) SmsUdhUtil.getTotalSize(myUdhElements));
129
130 try
131 {
132 for (int i = 0; i < myUdhElements.length; i++)
133 {
134 myUdhElements[i].writeTo(baos);
135 }
136 }
137 catch (IOException ioe)
138 {
139
140 throw new RuntimeException("Failed to write to ByteArrayOutputStream");
141 }
142
143 return baos.toByteArray();
144 }
145
146 /***
147 * Sets the user data field of the message.
148 *
149 * @param theUd
150 * The content
151 * @param theUdLength
152 * The length, can be in septets or octets depending on the DCS
153 * @param theDataCodingScheme
154 * The data coding scheme
155 */
156 public void setUserData(byte[] theUd, int theUdLength, SmsDcs theDataCodingScheme)
157 {
158 myUd = new SmsUserData(theUd, theUdLength, theDataCodingScheme);
159 }
160
161 /***
162 * Sets the user data field of the message.
163 *
164 * @param theUd
165 * The content
166 */
167 public void setUserData(SmsUserData theUd)
168 {
169 myUd = theUd;
170 }
171
172 /***
173 * Returns the user data part of the message.
174 *
175 * @return UD field
176 */
177 public SmsUserData getUserData()
178 {
179 return myUd;
180 }
181
182 /***
183 * Returns the dcs.
184 *
185 * @return dcs
186 */
187 public SmsDcs getDcs()
188 {
189 return myUd.getDcs();
190 }
191 }