19439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/*
28949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta * Copyright (c) 2014 The Android Open Source Project
39439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc.
49439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
59439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * All rights reserved.
69439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
79439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Redistribution and use in source and binary forms, with or without
89439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * modification, are permitted provided that the following conditions are met:
99439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions of source code must retain the above copyright notice,
119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer.
129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice,
149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer in the documentation
159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * and/or other materials provided with the distribution.
169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors
189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * may be used to endorse or promote products derived from this software
199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * without specific prior written permission.
209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * POSSIBILITY OF SUCH DAMAGE.
329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellypackage javax.obex;
359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
362e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.ByteArrayOutputStream;
372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.IOException;
382e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.util.Calendar;
3925f2c4cd953764008d04147a14d990c06a8154e5Oscar Montemayorimport java.security.SecureRandom;
409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/**
429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * This class implements the javax.obex.HeaderSet interface for OBEX over
43238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde * RFCOMM or OBEX over l2cap.
442e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly * @hide
459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellypublic final class HeaderSet {
479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
4905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Count header. This allows the connection statement to
5005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * tell the server how many objects it plans to send or retrieve.
519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>COUNT</code> is 0xC0 (192).
539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int COUNT = 0xC0;
559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Name header. This specifies the name of the object.
589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>NAME</code> is 0x01 (1).
609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int NAME = 0x01;
629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Type header. This allows a request to specify the
659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * type of the object (e.g. text, html, binary, etc.).
669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TYPE</code> is 0x42 (66).
689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TYPE = 0x42;
709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
7205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Length header. This is the length of the object in
739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * bytes.
749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>LENGTH</code> is 0xC3 (195).
769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int LENGTH = 0xC3;
789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
8005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Time header using the ISO 8601 standards. This is the
8105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * preferred time header.
829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TIME_ISO_8601</code> is 0x44 (68).
849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TIME_ISO_8601 = 0x44;
869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
8805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Time header using the 4 byte representation. This is
8905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * only included for backwards compatibility. It represents the number of
9005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * seconds since January 1, 1970.
919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TIME_4_BYTE</code> is 0xC4 (196).
939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TIME_4_BYTE = 0xC4;
959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
9705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Description header. This is a text description of the
9805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * object.
999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>DESCRIPTION</code> is 0x05 (5).
1019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int DESCRIPTION = 0x05;
1039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
10505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Target header. This is the name of the service an
1069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * operation is targeted to.
1079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TARGET</code> is 0x46 (70).
1099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TARGET = 0x46;
1119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
11305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX HTTP header. This allows an HTTP 1.X header to be
1149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * included in a request or reply.
1159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>HTTP</code> is 0x47 (71).
1179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int HTTP = 0x47;
1199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX BODY header.
1223998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>BODY</code> is 0x48 (72).
1243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int BODY = 0x48;
1263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX End of BODY header.
1293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>BODY</code> is 0x49 (73).
1313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int END_OF_BODY = 0x49;
1333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
13505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Who header. Identifies the OBEX application to
1369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * determine if the two peers are talking to each other.
1379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>WHO</code> is 0x4A (74).
1399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int WHO = 0x4A;
1419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX Connection ID header. Identifies used for OBEX
1443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * connection multiplexing.
1459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>CONNECTION_ID</code> is 0xCB (203).
1479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int CONNECTION_ID = 0xCB;
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
15205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Application Parameter header. This header specifies
1539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * additional application request and response information.
1549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>APPLICATION_PARAMETER</code> is 0x4C (76).
1569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int APPLICATION_PARAMETER = 0x4C;
1589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX authentication digest-challenge.
1613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>AUTH_CHALLENGE</code> is 0x4D (77).
1633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int AUTH_CHALLENGE = 0x4D;
1653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX authentication digest-response.
1683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>AUTH_RESPONSE</code> is 0x4E (78).
1703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int AUTH_RESPONSE = 0x4E;
1723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
17405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Object Class header. This header specifies the OBEX
17505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * object class of the object.
1763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>OBJECT_CLASS</code> is 0x4F (79).
1783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int OBJECT_CLASS = 0x4F;
1803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
181238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    /**
182238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * Represents the OBEX Single Response Mode (SRM). This header is used
183238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * for Single response mode, introduced in OBEX 1.5.
184238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * <P>
185238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * The value of <code>SINGLE_RESPONSE_MODE</code> is 0x97 (151).
186238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     */
187238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    public static final int SINGLE_RESPONSE_MODE = 0x97;
188238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde
189238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    /**
190238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * Represents the OBEX Single Response Mode Parameters. This header is used
191238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * for Single response mode, introduced in OBEX 1.5.
192238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * <P>
193238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     * The value of <code>SINGLE_RESPONSE_MODE_PARAMETER</code> is 0x98 (152).
194238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde     */
195238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    public static final int SINGLE_RESPONSE_MODE_PARAMETER = 0x98;
196238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde
1973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long mCount; // 4 byte unsigned integer
1989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mName; // null terminated Unicode text string
2009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2018949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    private boolean mEmptyName;
2028949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta
2033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mType; // null terminated ASCII text string
2049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long mLength; // 4 byte unsigend integer
2069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Calendar mIsoTime; // String of the form YYYYMMDDTHHMMSSZ
2089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Calendar mByteTime; // 4 byte unsigned integer
2109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mDescription; // null terminated Unicode text String
2129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mTarget; // byte sequence
2149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mHttpHeader; // byte sequence
2169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2173998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mWho; // length prefixed byte sequence
2189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mAppParam; // byte sequence of the form tag length value
2209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mObjectClass; // byte sequence
2229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
223238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    private String[] mUnicodeUserDefined; // null terminated unicode string
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[][] mSequenceUserDefined; // byte sequence user defined
2269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Byte[] mByteUserDefined; // 1 byte
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long[] mIntegerUserDefined; // 4 byte unsigned integer
2309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
231238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    private SecureRandom mRandom = null;
232238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde
233238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    private Byte mSingleResponseMode; // byte to indicate enable/disable/support for SRM
234238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde
235238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde    private Byte mSrmParam; // byte representing the SRM parameters - only "wait"
236238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                            // is supported by Bluetooth
2379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
23805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    /*package*/ byte[] nonce;
2399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
24005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mAuthChall; // The authentication challenge header
24105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
24205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mAuthResp; // The authentication response header
24305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
24405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mConnectionID; // THe connection ID
24505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
24605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public int responseCode;
2479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Creates new <code>HeaderSet</code> object.
2509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param size the max packet size for this connection
2519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public HeaderSet() {
2533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mUnicodeUserDefined = new String[16];
2543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mSequenceUserDefined = new byte[16][];
2553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mByteUserDefined = new Byte[16];
2563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mIntegerUserDefined = new Long[16];
2579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        responseCode = -1;
2589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2618949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * Sets flag for special "value" of NAME header which should be empty. This
2628949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * is not the same as NAME header with empty string in which case it will
2638949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * have length of 5 bytes. It should be 3 bytes with only header id and
2648949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * length field.
2658949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     */
2668949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    public void setEmptyNameHeader() {
2678949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta        mName = null;
2688949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta        mEmptyName = true;
2698949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    }
2708949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta
2718949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    /**
2728949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * Gets flag for special "value" of NAME header which should be empty. See
2738949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     * above.
2748949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta     */
2758949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    public boolean getEmptyNameHeader() {
2768949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta        return mEmptyName;
2778949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    }
2788949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta
2798949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta    /**
28005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sets the value of the header identifier to the value provided. The type
2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of object must correspond to the Java type defined in the description of
28205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * this interface. If <code>null</code> is passed as the
2839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>headerValue</code> then the header will be removed from the set of
2849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * headers to include in the next request.
2859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerID the identifier to include in the message
2869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerValue the value of the header identifier
28705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IllegalArgumentException if the header identifier provided is not
28805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         one defined in this interface or a user-defined header; if the
28905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         type of <code>headerValue</code> is not the correct Java type as
29005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         defined in the description of this interface\
2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void setHeader(int headerID, Object headerValue) {
2939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        long temp = -1;
2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        switch (headerID) {
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case COUNT:
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!(headerValue instanceof Long)) {
2989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
2993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mCount = null;
3009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
3019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Count must be a Long");
3039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                temp = ((Long)headerValue).longValue();
3059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Count must be between 0 and 0xFFFFFFFF");
3079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mCount = (Long)headerValue;
3099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case NAME:
3119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
3129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Name must be a String");
3139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3148949bfb90c415629dbd0e30d25003fb3e0375fb5Hemant Gupta                mEmptyName = false;
3153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mName = (String)headerValue;
3169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TYPE:
3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
3199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Type must be a String");
3209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mType = (String)headerValue;
3229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case LENGTH:
3249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!(headerValue instanceof Long)) {
3259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
3263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mLength = null;
3279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
3289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Length must be a Long");
3309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                temp = ((Long)headerValue).longValue();
3329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Length must be between 0 and 0xFFFFFFFF");
3349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mLength = (Long)headerValue;
3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_ISO_8601:
3389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof Calendar))) {
3399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Time ISO 8601 must be a Calendar");
3409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mIsoTime = (Calendar)headerValue;
3429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_4_BYTE:
3449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof Calendar))) {
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Time 4 Byte must be a Calendar");
3469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mByteTime = (Calendar)headerValue;
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case DESCRIPTION:
3509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Description must be a String");
3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mDescription = (String)headerValue;
3549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TARGET:
3569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mTarget = null;
3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Target must be a byte array");
3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mTarget = new byte[((byte[])headerValue).length];
3633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mTarget, 0, mTarget.length);
3649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case HTTP:
3689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mHttpHeader = null;
3709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("HTTP must be a byte array");
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mHttpHeader = new byte[((byte[])headerValue).length];
3753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mHttpHeader, 0, mHttpHeader.length);
3769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case WHO:
3809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mWho = null;
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("WHO must be a byte array");
3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mWho = new byte[((byte[])headerValue).length];
3873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mWho, 0, mWho.length);
3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case OBJECT_CLASS:
3929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mObjectClass = null;
3949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Object Class must be a byte array");
3979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mObjectClass = new byte[((byte[])headerValue).length];
3993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mObjectClass, 0, mObjectClass.length);
4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case APPLICATION_PARAMETER:
4049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
4053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mAppParam = null;
4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
4099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Application Parameter must be a byte array");
4109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
4113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mAppParam = new byte[((byte[])headerValue).length];
4123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mAppParam, 0, mAppParam.length);
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
416238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            case SINGLE_RESPONSE_MODE:
417238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                if (headerValue == null) {
418238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    mSingleResponseMode = null;
419238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                } else {
420238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    if (!(headerValue instanceof Byte)) {
421238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                        throw new IllegalArgumentException(
422238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                                "Single Response Mode must be a Byte");
423238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    } else {
424238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                        mSingleResponseMode = (Byte)headerValue;
425238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    }
426238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                }
427238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                break;
428238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            case SINGLE_RESPONSE_MODE_PARAMETER:
429238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                if (headerValue == null) {
430238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    mSrmParam = null;
431238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                } else {
432238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    if (!(headerValue instanceof Byte)) {
433238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                        throw new IllegalArgumentException(
434238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                                "Single Response Mode Parameter must be a Byte");
435238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    } else {
436238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                        mSrmParam = (Byte)headerValue;
437238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                    }
438238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                }
439238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                break;
4409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            default:
4419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Unicode String user Defined
4429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x30) && (headerID <= 0x3F)) {
4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((headerValue != null) && (!(headerValue instanceof String))) {
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
4459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Unicode String User Defined must be a String");
4469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mUnicodeUserDefined[headerID - 0x30] = (String)headerValue;
4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte sequence user defined value
4529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x70) && (headerID <= 0x7F)) {
4539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
4553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mSequenceUserDefined[headerID - 0x70] = null;
4569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
4579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        if (!(headerValue instanceof byte[])) {
4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            throw new IllegalArgumentException(
4599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                    "Byte Sequence User Defined must be a byte array");
4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        } else {
4613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mSequenceUserDefined[headerID - 0x70] = new byte[((byte[])headerValue).length];
4623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            System.arraycopy(headerValue, 0, mSequenceUserDefined[headerID - 0x70],
4633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                                    0, mSequenceUserDefined[headerID - 0x70].length);
4649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Byte user Defined
4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xB0) && (headerID <= 0xBF)) {
4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((headerValue != null) && (!(headerValue instanceof Byte))) {
4719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("ByteUser Defined must be a Byte");
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mByteUserDefined[headerID - 0xB0] = (Byte)headerValue;
4749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that is was not the 4 byte unsigned integer user
4789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // defined header
4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xF0) && (headerID <= 0xFF)) {
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof Long)) {
4819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        if (headerValue == null) {
4823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mIntegerUserDefined[headerID - 0xF0] = null;
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            break;
4849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Integer User Defined must be a Long");
4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    temp = ((Long)headerValue).longValue();
4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Integer User Defined must be between 0 and 0xFFFFFFFF");
4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mIntegerUserDefined[headerID - 0xF0] = (Long)headerValue;
4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IllegalArgumentException("Invalid Header Identifier");
4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
50005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Retrieves the value of the header identifier provided. The type of the
5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Object returned is defined in the description of this interface.
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerID the header identifier whose value is to be returned
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the value of the header provided or <code>null</code> if the
50405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         header identifier specified is not part of this
50505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object
50605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IllegalArgumentException if the <code>headerID</code> is not one
50705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         defined in this interface or any of the user-defined headers
5082e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
50905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the operation or if the connection has been closed
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public Object getHeader(int headerID) throws IOException {
5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        switch (headerID) {
5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case COUNT:
5153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mCount;
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case NAME:
5173998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mName;
5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TYPE:
5193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mType;
5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case LENGTH:
5213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mLength;
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_ISO_8601:
5233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mIsoTime;
5249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_4_BYTE:
5253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mByteTime;
5269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case DESCRIPTION:
5273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mDescription;
5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TARGET:
5293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mTarget;
5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case HTTP:
5313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mHttpHeader;
5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case WHO:
5333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mWho;
534fe3807a5b23f54f6539436d71aa0cd931a2b76f0Matthew Xie            case CONNECTION_ID:
535fe3807a5b23f54f6539436d71aa0cd931a2b76f0Matthew Xie                return mConnectionID;
5369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case OBJECT_CLASS:
5373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mObjectClass;
5389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case APPLICATION_PARAMETER:
5393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mAppParam;
540238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            case SINGLE_RESPONSE_MODE:
541238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                return mSingleResponseMode;
542238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            case SINGLE_RESPONSE_MODE_PARAMETER:
543238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde                return mSrmParam;
5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            default:
5459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Unicode String user Defined
5469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x30) && (headerID <= 0x3F)) {
5473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mUnicodeUserDefined[headerID - 0x30];
5489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte sequence user defined header
5509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x70) && (headerID <= 0x7F)) {
5513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mSequenceUserDefined[headerID - 0x70];
5529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte user defined header
5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xB0) && (headerID <= 0xBF)) {
5553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mByteUserDefined[headerID - 0xB0];
5569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // Verify that it was not a integer user defined header
5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xF0) && (headerID <= 0xFF)) {
5593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mIntegerUserDefined[headerID - 0xF0];
5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IllegalArgumentException("Invalid Header Identifier");
5629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Retrieves the list of headers that may be retrieved via the
56705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * <code>getHeader</code> method that will not return <code>null</code>. In
56805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * other words, this method returns all the headers that are available in
56905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * this object.
5709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @see #getHeader
5719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the array of headers that are set in this object or
57205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>null</code> if no headers are available
5732e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
57405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the operation or the connection has been closed
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int[] getHeaderList() throws IOException {
5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ByteArrayOutputStream out = new ByteArrayOutputStream();
5789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mCount != null) {
5809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(COUNT);
5819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mName != null) {
5839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(NAME);
5849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mType != null) {
5869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TYPE);
5879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mLength != null) {
5899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(LENGTH);
5909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mIsoTime != null) {
5929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TIME_ISO_8601);
5939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mByteTime != null) {
5959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TIME_4_BYTE);
5969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mDescription != null) {
5989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(DESCRIPTION);
5999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mTarget != null) {
6019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TARGET);
6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mHttpHeader != null) {
6049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(HTTP);
6059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mWho != null) {
6079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(WHO);
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mAppParam != null) {
6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(APPLICATION_PARAMETER);
6119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mObjectClass != null) {
6139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(OBJECT_CLASS);
6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
615238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        if(mSingleResponseMode != null) {
616238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            out.write(SINGLE_RESPONSE_MODE);
617238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        }
618238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        if(mSrmParam != null) {
619238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            out.write(SINGLE_RESPONSE_MODE_PARAMETER);
620238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        }
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0x30; i < 0x40; i++) {
6233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mUnicodeUserDefined[i - 0x30] != null) {
6249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0x70; i < 0x80; i++) {
6293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mSequenceUserDefined[i - 0x70] != null) {
6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
6319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0xB0; i < 0xC0; i++) {
6353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mByteUserDefined[i - 0xB0] != null) {
6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
6379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0xF0; i < 0x100; i++) {
6413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mIntegerUserDefined[i - 0xF0] != null) {
6429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
6439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] headers = out.toByteArray();
6479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        out.close();
6489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((headers == null) || (headers.length == 0)) {
6509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return null;
6519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int[] result = new int[headers.length];
6549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0; i < headers.length; i++) {
6559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // Convert the byte to a positive integer.  That is, an integer
6569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // between 0 and 256.
6579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            result[i] = headers[i] & 0xFF;
6589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return result;
6619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
66405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sets the authentication challenge header. The <code>realm</code> will be
66505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * encoded based upon the default encoding scheme used by the implementation
66605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * to encode strings. Therefore, the encoding scheme used to encode the
66705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * <code>realm</code> is application dependent.
6689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param realm a short description that describes what password to use; if
66905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        <code>null</code> no realm will be sent in the authentication
67005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        challenge header
6719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param userID if <code>true</code>, a user ID is required in the reply;
67205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        if <code>false</code>, no user ID is required
6739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param access if <code>true</code> then full access will be granted if
67405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        successful; if <code>false</code> then read-only access will be
67505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        granted if successful
6763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * @throws IOException
6779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public void createAuthenticationChallenge(String realm, boolean userID, boolean access)
6793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throws IOException {
6809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
68105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        nonce = new byte[16];
682238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        if(mRandom == null) {
683238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde            mRandom = new SecureRandom();
684238e0f934f1f47263b384bc745ae0678c777130dCasper Bonde        }
68505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        for (int i = 0; i < 16; i++) {
68605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            nonce[i] = (byte)mRandom.nextInt();
6879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
68805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
68905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        mAuthChall = ObexHelper.computeAuthenticationChallenge(nonce, realm, access, userID);
6909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
69305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Returns the response code received from the server. Response codes are
69405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * defined in the <code>ResponseCodes</code> class.
6959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @see ResponseCodes
6969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the response code retrieved from the server
6972e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
69805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the transaction; if this method is called on a
69905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object created by calling
70005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>createHeaderSet()</code> in a <code>ClientSession</code>
70105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         object; if this object was created by an OBEX server
7029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
7039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int getResponseCode() throws IOException {
7049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (responseCode == -1) {
7059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("May not be called on a server");
7069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
7079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return responseCode;
7089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
7099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
7109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
711