View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.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   * ***** END LICENSE BLOCK ***** */
23  package org.marre.sms.nokia;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  import org.marre.sms.SmsConstants;
31  import org.marre.sms.SmsPortAddressedMessage;
32  import org.marre.sms.SmsUserData;
33  
34  /***
35   * Baseclass for Nokia Multipart Messages
36   * <p>
37   * Baseclass for messages that rely on the Nokia Multipart Messages
38   * 
39   * @author Markus Eriksson
40   * @version $Id: NokiaMultipartMessage.java,v 1.11 2004/11/20 19:02:34 c95men Exp $
41   */
42  abstract class NokiaMultipartMessage extends SmsPortAddressedMessage
43  {
44      private List myParts = new LinkedList();
45  
46      /***
47       * Creates a Nokia Multipart Message
48       */
49      protected NokiaMultipartMessage()
50      {
51          super(SmsConstants.PORT_NOKIA_MULTIPART_MESSAGE, 0);
52      }
53  
54      /***
55       * Adds a part to this multipart message
56       * 
57       * @param theItemType
58       *            Type
59       * @param data
60       *            Content
61       */
62      protected void addMultipart(byte theItemType, byte[] data)
63      {
64          myParts.add(new NokiaPart(theItemType, data));
65      }
66  
67      /***
68       * Removes all parts from the message
69       */
70      protected void clear()
71      {
72          myParts.clear();
73      }
74  
75      public SmsUserData getUserData()
76      {
77          ByteArrayOutputStream baos = new ByteArrayOutputStream(140);
78  
79          // Payload
80  
81          try
82          {
83              // Header or something...
84              baos.write(0x30);
85  
86              // Loop through all multiparts and add them
87              for (int i = 0; i < myParts.size(); i++)
88              {
89                  NokiaPart part = (NokiaPart) myParts.get(i);
90                  byte[] data = part.getData();
91  
92                  // Type - 1 octet
93                  baos.write(part.getItemType());
94                  // Length - 2 octets
95                  baos.write((byte) ((data.length >> 8) & 0xff));
96                  baos.write((byte) (data.length & 0xff));
97                  // Data - n octets
98                  baos.write(data);
99              }
100 
101             baos.close();
102         }
103         catch (IOException ex)
104         {
105             // Should not happen!
106         }
107 
108         return new SmsUserData(baos.toByteArray());
109     }
110 }