FileDocCategorySizeDatePackage
ChunkHandler.javaAPI DocAndroid 1.5 API3887Wed May 06 22:41:02 BST 2009org.apache.harmony.dalvik.ddmc

ChunkHandler

public abstract class ChunkHandler extends Object
Handle a chunk of data sent from a DDM server. To handle a chunk type, sub-class ChunkHandler and register your class with DdmServer.

Fields Summary
public static final ByteOrder
CHUNK_ORDER
public static final int
CHUNK_FAIL
Constructors Summary
public ChunkHandler()



      
Methods Summary
public abstract voidconnected()
Called when the DDM server connects. The handler is allowed to send messages to the server.

public static ChunkcreateFailChunk(int errorCode, java.lang.String msg)
Create a FAIL chunk. The "handleChunk" methods can use this to return an error message when they are not able to process a chunk.

        if (msg == null)
            msg = "";

        ByteBuffer out = ByteBuffer.allocate(8 + msg.length() * 2);
        out.order(ChunkHandler.CHUNK_ORDER);
        out.putInt(errorCode);
        out.putInt(msg.length());
        putString(out, msg);

        return new Chunk(CHUNK_FAIL, out);
    
public abstract voiddisconnected()
Called when the DDM server disconnects. Can be used to disable periodic transmissions or clean up saved state.

public static java.lang.StringgetString(java.nio.ByteBuffer buf, int len)
Utility function to copy a String out of a ByteBuffer. This is here because multiple chunk handlers can make use of it, and there's nowhere better to put it.

        char[] data = new char[len];
        for (int i = 0; i < len; i++)
            data[i] = buf.getChar();
        return new String(data);
    
public abstract ChunkhandleChunk(Chunk request)
Handle a single chunk of data. "request" includes the type and the chunk payload. Returns a response in a Chunk.

public static java.lang.Stringname(int type)
Convert an integer type to a 4-character string.

        char[] ascii = new char[4];

        ascii[0] = (char) ((type >> 24) & 0xff);
        ascii[1] = (char) ((type >> 16) & 0xff);
        ascii[2] = (char) ((type >> 8) & 0xff);
        ascii[3] = (char) (type & 0xff);

        return new String(ascii);
    
public static voidputString(java.nio.ByteBuffer buf, java.lang.String str)
Utility function to copy a String into a ByteBuffer.

        int len = str.length();
        for (int i = 0; i < len; i++)
            buf.putChar(str.charAt(i));
    
public static inttype(java.lang.String typeName)
Convert a 4-character string to a 32-bit type.

        int val = 0;

        if (typeName.length() != 4)
            throw new RuntimeException();

        for (int i = 0; i < 4; i++) {
            val <<= 8;
            val |= (byte) typeName.charAt(i);
        }

        return val;
    
public static java.nio.ByteBufferwrapChunk(Chunk request)
Utility function to wrap a ByteBuffer around a Chunk.

        ByteBuffer in;
        
        in = ByteBuffer.wrap(request.data, request.offset, request.length);
        in.order(CHUNK_ORDER);
        return in;