FileDocCategorySizeDatePackage
HeaderSetImpl.javaAPI DocphoneME MR2 API (J2ME)8657Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.obex

HeaderSetImpl

public class HeaderSetImpl extends Object implements javax.obex.HeaderSet

Fields Summary
private static final boolean
DEBUG
Debug information, should be false for RR.
public static final int
OWNER_SERVER
public static final int
OWNER_CLIENT
public static final int
OWNER_SERVER_USER
public static final int
OWNER_CLIENT_USER
static final int
TYPE_UNICODE
static final int
TYPE_BYTEARRAY
static final int
TYPE_BYTE
static final int
TYPE_LONG
static final int
TYPE_SPECIAL_TIME_4
static final int
TYPE_SPECIAL_TIME_ISO
static final int
TYPE_SPECIAL_TYPE
static final int
TYPE_UNSUPPORTED
static final int
TYPE_AUTH_CHALLENGE
static final int
TYPE_AUTH_RESPONSE
int
owner
int
packetType
private Hashtable
headers
Vector
challenges
Constructors Summary
public HeaderSetImpl(int owner)


       
        this.owner = owner;
        headers = new Hashtable(5);
    
HeaderSetImpl(HeaderSetImpl h)
Makes private copy of the headers.

        this.packetType = h.packetType;
        this.owner = h.owner;
        headers = new Hashtable(5);
        merge(h);
    
Methods Summary
public voidcreateAuthenticationChallenge(java.lang.String realm, boolean userID, boolean access)


        ObexAuth auth = ObexAuth.createChallenge(realm, userID, access);
        challenges.addElement(auth);
    
public java.lang.ObjectgetHeader(int headerID)

        Object value = headers.get(new Integer(headerID));

        if (value != null) {
            return value;
        }

        // check validness of headerID
        int type = internalType(headerID);

        if (type >= TYPE_UNSUPPORTED) {
            throw new IllegalArgumentException("bad header id");
        }
        return null;
    
public int[]getHeaderList()

        synchronized (headers) {
            if (headers.isEmpty()) {
                return null;
            }
            Enumeration keys = headers.keys();
            int[] ids = new int[headers.size()];
            int i = 0;

            while (keys.hasMoreElements()) {
                Object obj = keys.nextElement();
                ids[i++] = ((Integer)obj).intValue();
            }
            return ids;
        }
    
public intgetResponseCode()

        if (owner != OWNER_CLIENT) {
            throw new IOException("invalid use");
        }
        if (packetType == ObexPacketStream.OPCODE_CONTINUE) {
            throw new IOException("operation not finished");
        }
        return packetType;
    
static final intinternalType(int headerID)

        if ((headerID & ~0xFF) != 0) {
            return TYPE_UNSUPPORTED;
        }
        if ((headerID & 0x30) == 0x30) {
            // user defined
            return headerID >> 6;
        }

        switch (headerID) {
            case HeaderSet.TIME_ISO_8601:
                return TYPE_SPECIAL_TIME_ISO;

            case HeaderSet.TIME_4_BYTE:
                return TYPE_SPECIAL_TIME_4;

            case HeaderSet.TYPE:
                return TYPE_SPECIAL_TYPE;

            case HeaderSet.COUNT:
            case HeaderSet.LENGTH:
                return TYPE_LONG;

            case HeaderSet.NAME:
            case HeaderSet.DESCRIPTION:
                return TYPE_UNICODE;

            case HeaderSet.TARGET:
            case HeaderSet.HTTP:
            case HeaderSet.WHO:
            case HeaderSet.OBJECT_CLASS:
            case HeaderSet.APPLICATION_PARAMETER:
                return TYPE_BYTEARRAY;

            case ObexPacketStream.HEADER_AUTH_CHALLENGE:
                return TYPE_AUTH_CHALLENGE;

            case ObexPacketStream.HEADER_AUTH_RESPONSE:
                return TYPE_AUTH_RESPONSE;

            default:
                return TYPE_UNSUPPORTED;
        }
    
booleanisSendable()

        return owner >= OWNER_SERVER_USER;
    
voidmerge(com.sun.kvem.jsr082.obex.HeaderSetImpl h)
Adds headers from h to this HeaderSet.

        int[] idList = h.getHeaderList();

        if (idList == null) {
            return;
        }

        for (int i = 0; i < idList.length; i++) {
            int id = idList[i];
            Object val = h.getHeader(id);
            int type = internalType(id);
            switch (type) {
                case TYPE_UNICODE:
                case TYPE_BYTE:
                case TYPE_LONG:
                case TYPE_SPECIAL_TYPE:
                    setHeader(id, val);
                    break;

                case TYPE_BYTEARRAY:
                    byte[] array = (byte[]) val;
                    byte[] copy = new byte[array.length];
                    System.arraycopy(array, 0, copy, 0, array.length);
                    setHeader(id, (Object) copy);
                    break;

                case TYPE_SPECIAL_TIME_4:
                case TYPE_SPECIAL_TIME_ISO:
                    Calendar cal = (Calendar) val;
                    Calendar calCopy = Calendar.getInstance();
                    calCopy.setTime(cal.getTime());
                    setHeader(id, (Object) calCopy);
                    break;

                default:
                    // no default
                    if (DEBUG) {
                        System.out.println("Debug: unknown header id");
                    }
            }
        }
    
public voidsetHeader(int headerID, java.lang.Object headerValue)

        int type = internalType(headerID);
        String errormsg = "incompatible header object";

        if (type >= TYPE_UNSUPPORTED) {
            throw new IllegalArgumentException("bad header id");
        }

        if (headerValue == null) {
            synchronized (headers) {
                headers.remove(new Integer(headerID));
            }
            return;
        }

        boolean fail = false;
        switch (type) {
            case TYPE_UNICODE:
                if (!(headerValue instanceof String)) fail = true;
                break;

            case TYPE_BYTEARRAY:
                if (!(headerValue instanceof byte[])) fail = true;
                break;

            case TYPE_BYTE:
                if (!(headerValue instanceof Byte)) fail = true;
                break;

            case TYPE_LONG:
                if (!(headerValue instanceof Long)) {
                    fail = true;
                    break;
                }
                long value = ((Long)headerValue).longValue();

                if (value < 0L || value > 0xFFFFFFFFL)  {
                    errormsg = "long value out of range 0 .. 0xFFFFFFFF";
                    fail = true;
                }
                break;

            case TYPE_SPECIAL_TIME_4:
                if (!(headerValue instanceof Calendar)) fail = true;
                break;

            case TYPE_SPECIAL_TIME_ISO:
                if (!(headerValue instanceof Calendar)) fail = true;
                break;

            case TYPE_SPECIAL_TYPE:
                if (!(headerValue instanceof String)) fail = true;
                break;
            // no default
        }

        if (fail) {
            throw new IllegalArgumentException(errormsg);
        }
        synchronized (headers) {
            headers.put(new Integer(headerID), headerValue);
        }