FileDocCategorySizeDatePackage
DatagramObject.javaAPI DocJ2ME MIDP 2.08157Thu Nov 07 12:02:22 GMT 2002com.sun.midp.io.j2me.datagram

DatagramObject

public class DatagramObject extends com.sun.cldc.io.GeneralBase implements Datagram
Implements a UDP datagram for the UDP datagram connection.

Fields Summary
private static final int
MAX_HOST_LENGTH
Length of the hostname buffer.
private byte[]
buffer
Buffer to be used.
private int
offset
Where to start sending or receiving data.
private int
length
Number of bytes to send or receive.
String
address
Datagram address as a URL.
int
ipNumber
Raw IPv4 address.
int
port
UDP port
private int
readWritePosition
Current read/write position in buffer.
Constructors Summary
public DatagramObject(byte[] buf, int len)
Create a Datagram Object.

param
buf The buffer to be used in the datagram
param
len The length of the buffer to be allocated for the datagram


                                                                                      
         
        setData(buf, 0, len);
    
Methods Summary
public java.lang.StringgetAddress()
Get the address in the datagram.

return
the address in string form, or null if no address was set
see
#setAddress

        return address;
    
public byte[]getData()
Get the buffer.

return
the data buffer
see
#setData

        return buffer;
    
public intgetLength()
Get the a number that is either the number of bytes to send or the number of bytes received.

return
the length of the data
see
#setLength

        return length;
    
public intgetOffset()
Get the offset.

return
the offset into the data buffer

        return offset;
    
public intread()
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

return
the next byte of data, or -1 if the end of the stream is reached.

        if (readWritePosition >= length) {
            return -1;
        }

        return buffer[offset + readWritePosition++] & 0xFF;
    
public voidreset()
Zero the read/write pointer as well as the offset and length parameters.

        readWritePosition = 0;
        offset = 0;
        length = 0;
    
public voidsetAddress(java.lang.String addr)
Set datagram address. Must a "datagram://" URI.

param
addr the new target address as a URL
exception
IllegalArgumentException if the address is not valid
see
#getAddress

        HttpUrl url;
        int temp;

	if (addr == null)  {
            throw new IllegalArgumentException("Invalid address");
        }

        url = new HttpUrl(addr);

        if (url.scheme == null || !url.scheme.equals("datagram")) {
            throw new IllegalArgumentException("Invalid scheme");
        }

        /*
         * Since we reused the HttpUrl parser, we must make sure that
         * there was nothing past the authority in the URL.
         */
        if (url.path != null || url.query != null || url.fragment != null) {
            throw new IllegalArgumentException("Malformed address");
        }

        port = url.port;

        if (url.host == null) {
            throw new IllegalArgumentException("Missing host");
        }
            
        if (port == -1) {
            throw new IllegalArgumentException("Missing port");
        }

        temp = Protocol.getIpNumber(Util.toCString(url.host));
        if (temp == -1) {
            throw new IllegalArgumentException("Invalid host");
        }

        ipNumber = temp;
        address = addr;
    
public voidsetAddress(Datagram reference)
Set datagram address, copying the address from another datagram.

param
reference the datagram who's address will be copied as the new target address for this datagram.
exception
IllegalArgumentException if the address is not valid
see
#getAddress

        setAddress(reference.getAddress());
    
public voidsetData(byte[] buf, int off, int len)
Set the buffer, offset and length.

param
buf the data buffer
param
off the offset into the data buffer
param
len the length of the data in the buffer
exception
IllegalArgumentException if the length or offset fall outside the buffer
see
#getData

	/*
	 * Check that the offset and length are valid.
	 *   - must be positive
	 *   - must not exceed buffer length
	 *   - must have valid buffer
	 */
        if (len < 0 || off < 0 ||
	    (buf == null) ||
	    (off > 0 && off == buf.length) ||
            ((len + off) > buf.length)||
            ((len + off) < 0)) {
            throw new IllegalArgumentException("Illegal length or offset");
        }

        buffer = buf;
        offset = off;
        length = len;
    
public voidsetLength(int len)
Set the length. Which can represent either the number of bytes to send or the maxium number of bytes to receive.

param
len the new length of the data
exception
IllegalArgumentException if the length is negative or larger than the buffer
see
#getLength

        setData(buffer, offset, len);
    
public longskip(long n)
A more effient skip than the one in GeneralBase. Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

param
n the number of bytes to be skipped.
return
the actual number of bytes skipped.
exception
IOException if an I/O error occurs.

        if (n < 0) {
            return 0;
        }

        if (readWritePosition >= length) {
            return 0;
        }

        int min = Math.min((int)n, length - readWritePosition);
        readWritePosition += min;
        return (min);
    
public voidwrite(int ch)
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of ch are ignored.

param
ch the byte.
exception
IOException if an I/O error occurs. In particular, an IOException may be thrown if the buffer is full.

        if (offset + readWritePosition >= buffer.length) {
            throw new IOException("Buffer full");
        }

        buffer[offset + readWritePosition++] = (byte)ch;
        length = readWritePosition;