View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
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          // Empty
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             // Shouldn't happen.
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 }