FileDocCategorySizeDatePackage
ServiceRecordSerializer.javaAPI DocphoneME MR2 API (J2ME)4950Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.bluetooth

ServiceRecordSerializer

public class ServiceRecordSerializer extends Object
Serializes ServiceRecord objects.

Fields Summary
static DataElementSerializer
des
Helper object used for data element serialization.
Constructors Summary
Methods Summary
public static synchronized ServiceRecordImplrestore(BluetoothNotifier notifier, byte[] data)
Restores previously serialized service record.

param
notifier notifier object the newly created record to be associated with
param
data serialized service record data
return
restored service record

        DataElement seq;
        try {
            seq = des.restore(data);
        } catch (IOException e) {
            return null;
        }
        Enumeration elements = (Enumeration)seq.getValue();
        int[] attrIDs = new int[seq.getSize() / 2];
        DataElement[] attrValues = new DataElement[attrIDs.length];
        for (int i = 0; i < attrIDs.length; i++) {
            attrIDs[i] = (int)((DataElement)elements.nextElement()).getLong();
            DataElement attrValue = (DataElement)elements.nextElement();
            DataElement newAttrValue;
            int dataType = attrValue.getDataType();
            switch (dataType) {
                case DataElement.BOOL:
                    newAttrValue = new DataElement(attrValue.getBoolean());
                    break;
                case DataElement.NULL:
                    newAttrValue = new DataElement(DataElement.NULL);
                    break;
                case DataElement.U_INT_1:
                case DataElement.U_INT_2:
                case DataElement.U_INT_4:
                case DataElement.INT_1:
                case DataElement.INT_2:
                case DataElement.INT_4:
                case DataElement.INT_8:
                    newAttrValue = new DataElement(dataType,
                            attrValue.getLong());
                    break;
                case DataElement.DATALT:
                case DataElement.DATSEQ:
                    Enumeration e = (Enumeration)attrValue.getValue();
                    newAttrValue = new DataElement(dataType);
                    while (e.hasMoreElements()) {
                        newAttrValue.addElement((DataElement)e.nextElement());
                    }
                    break;
                default:
                    newAttrValue = new DataElement(attrValue.getDataType(),
                            attrValue.getValue());
                    break;
            }
            attrValues[i] = newAttrValue;
        }
        return new ServiceRecordImpl(notifier, attrIDs, attrValues);
    
public static synchronized byte[]serialize(javax.bluetooth.ServiceRecord record)
Serializes given service record - creates an array of bytes representing data elements as described in Bluetooth Specification Version 1.2, vol 3, page 127.

param
record the service record to serialize
return
an array containing the serialized record

        des = new DataElementSerializer();
    
        DataElement seq = new DataElement(DataElement.DATSEQ);
        int[] attrIDs = record.getAttributeIDs();
        for (int i = 0; i < attrIDs.length; i++) {
            DataElement attrID = new DataElement(DataElement.U_INT_2,
                    attrIDs[i]);
            DataElement attrValue = record.getAttributeValue(attrIDs[i]);
            if (attrValue != null) {
                seq.addElement(attrID);
                seq.addElement(attrValue);
            }
        }
        try {
            return des.serialize(seq);
        } catch (IOException e) {
            return null;
        }