001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.kvem.jsr082.bluetooth;
027:
028: import java.io.IOException;
029: import java.util.Enumeration;
030: import javax.bluetooth.DataElement;
031: import javax.bluetooth.ServiceRecord;
032:
033: /**
034: * Serializes ServiceRecord objects.
035: */
036: public class ServiceRecordSerializer {
037:
038: /** Helper object used for data element serialization. */
039: static DataElementSerializer des;
040:
041: /** Initializes static fields. */
042: static {
043: des = new DataElementSerializer();
044: }
045:
046: /**
047: * Serializes given service record - creates an array of bytes representing
048: * data elements as described in Bluetooth Specification Version 1.2,
049: * vol 3, page 127.
050: *
051: * @param record the service record to serialize
052: * @return an array containing the serialized record
053: */
054: public static synchronized byte[] serialize(ServiceRecord record) {
055: DataElement seq = new DataElement(DataElement.DATSEQ);
056: int[] attrIDs = record.getAttributeIDs();
057: for (int i = 0; i < attrIDs.length; i++) {
058: DataElement attrID = new DataElement(DataElement.U_INT_2,
059: attrIDs[i]);
060: DataElement attrValue = record
061: .getAttributeValue(attrIDs[i]);
062: if (attrValue != null) {
063: seq.addElement(attrID);
064: seq.addElement(attrValue);
065: }
066: }
067: try {
068: return des.serialize(seq);
069: } catch (IOException e) {
070: return null;
071: }
072: }
073:
074: /**
075: * Restores previously serialized service record.
076: *
077: * @param notifier notifier object the newly created record
078: * to be associated with
079: * @param data serialized service record data
080: * @return restored service record
081: */
082: public static synchronized ServiceRecordImpl restore(
083: BluetoothNotifier notifier, byte[] data) {
084: DataElement seq;
085: try {
086: seq = des.restore(data);
087: } catch (IOException e) {
088: return null;
089: }
090: Enumeration elements = (Enumeration) seq.getValue();
091: int[] attrIDs = new int[seq.getSize() / 2];
092: DataElement[] attrValues = new DataElement[attrIDs.length];
093: for (int i = 0; i < attrIDs.length; i++) {
094: attrIDs[i] = (int) ((DataElement) elements.nextElement())
095: .getLong();
096: DataElement attrValue = (DataElement) elements
097: .nextElement();
098: DataElement newAttrValue;
099: int dataType = attrValue.getDataType();
100: switch (dataType) {
101: case DataElement.BOOL:
102: newAttrValue = new DataElement(attrValue.getBoolean());
103: break;
104: case DataElement.NULL:
105: newAttrValue = new DataElement(DataElement.NULL);
106: break;
107: case DataElement.U_INT_1:
108: case DataElement.U_INT_2:
109: case DataElement.U_INT_4:
110: case DataElement.INT_1:
111: case DataElement.INT_2:
112: case DataElement.INT_4:
113: case DataElement.INT_8:
114: newAttrValue = new DataElement(dataType, attrValue
115: .getLong());
116: break;
117: case DataElement.DATALT:
118: case DataElement.DATSEQ:
119: Enumeration e = (Enumeration) attrValue.getValue();
120: newAttrValue = new DataElement(dataType);
121: while (e.hasMoreElements()) {
122: newAttrValue.addElement((DataElement) e
123: .nextElement());
124: }
125: break;
126: default:
127: newAttrValue = new DataElement(attrValue.getDataType(),
128: attrValue.getValue());
129: break;
130: }
131: attrValues[i] = newAttrValue;
132: }
133: return new ServiceRecordImpl(notifier, attrIDs, attrValues);
134: }
135:
136: }
|