⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramobject.java

📁 已经移植好的java虚拟机
💻 JAVA
字号:
/* *  Copyright (c) 1999 Sun Microsystems, Inc., 901 San Antonio Road, *  Palo Alto, CA 94303, U.S.A.  All Rights Reserved. * *  Sun Microsystems, Inc. has intellectual property rights relating *  to the technology embodied in this software.  In particular, and *  without limitation, these intellectual property rights may include *  one or more U.S. patents, foreign patents, or pending *  applications.  Sun, Sun Microsystems, the Sun logo, Java, KJava, *  and all Sun-based and Java-based marks are trademarks or *  registered trademarks of Sun Microsystems, Inc.  in the United *  States and other countries. * *  This software is distributed under licenses restricting its use, *  copying, distribution, and decompilation.  No part of this *  software may be reproduced in any form by any means without prior *  written authorization of Sun and its licensors, if any. * *  FEDERAL ACQUISITIONS:  Commercial Software -- Government Users *  Subject to Standard License Terms and Conditions */package com.sun.cldc.io.j2se.datagram;import java.io.*;import java.net.*;import javax.microedition.io.*;import com.sun.cldc.io.j2se.*;import com.sun.cldc.io.*;/** * This class is required because the J2SE Datagram class is final. */public class DatagramObject extends GeneralBase implements Datagram {    DatagramPacket dgram;    String host;    int port;    public DatagramObject(DatagramPacket dpkt) {        dgram = dpkt;    }    public DatagramObject(int p) {        port  = p;        host  = "localhost";   //    dgram = new DatagramPacket(port, InetAddress.getByName(host));    }    public String getAddress() {        InetAddress addr = dgram.getAddress();        if(addr == null) {            return null;        } else {            return "datagram://" + addr.getHostName() + ":" + addr.getHostAddress();        }    }    public byte[] getData() {        return dgram.getData();    }    public int getOffset() {        return dgram.getOffset();    }    public int getLength() {        return dgram.getLength();    }    public void setAddress(String addr) throws IOException {        if(!addr.startsWith("datagram://")) {            throw new IllegalArgumentException("Invalid datagram address"+addr);        }        String address = addr.substring(11);        try {            host = Protocol.getAddress(address);            port = Protocol.getPort(address);            dgram.setAddress(InetAddress.getByName(host));            dgram.setPort(port);        } catch(NumberFormatException x) {            throw new IllegalArgumentException("Invalid datagram address"+addr);        } catch(UnknownHostException x) {            throw new IllegalArgumentException("Unknown host "+addr);        }    }    public void setAddress(Datagram reference) {        DatagramObject ref = (DatagramObject)reference;        try {            setAddress(ref.getAddress());        } catch(IOException x) {            throw new RuntimeException("IOException in DatagramObject::setAddress");        }    }    public void setData(byte[] buffer, int offset, int len) {        dgram.setData(buffer, offset, len);    }    public void setLength(int len) {        dgram.setLength(len);    }//// Read / write functions.//    int pointer;    public void reset() {        byte[] b = dgram.getData();        dgram.setData(b, 0, 0);        pointer = 0;    }    public long skip(long n) {        int len = dgram.getLength();        int min = Math.min((int)n, len - pointer);        pointer += min;        return (min);    }    public int read() throws IOException {        byte[] b = dgram.getData();        if(pointer >= dgram.getLength()) {            return -1;        }        return b[dgram.getOffset()+(pointer++)] & 0xFF;    }    public void write(int ch) throws IOException {        byte[] b = dgram.getData();        if(pointer >= b.length) {            throw new IndexOutOfBoundsException();        }        b[pointer++] = (byte)ch;        dgram.setData(b, 0, pointer);    }//// DataInput functions//    /**     * See the general contract of the <code>readFully</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @param      b   the buffer into which the data is read.     * @exception  EOFException  if this input stream reaches the end before     *               reading all the bytes.     * @exception  IOException   if an I/O error occurs.     */    public final void readFully(byte b[]) throws IOException {        readFully(b, 0, b.length);    }    /**     * See the general contract of the <code>readFully</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @param      b     the buffer into which the data is read.     * @param      off   the start offset of the data.     * @param      len   the number of bytes to read.     * @exception  EOFException  if this input stream reaches the end before     *               reading all the bytes.     * @exception  IOException   if an I/O error occurs.     */    public final void readFully(byte b[], int off, int len) throws IOException {        if (len < 0)            throw new IndexOutOfBoundsException();        int n = 0;        while (n < len) {            int ch = read();            if (ch < 0) {                throw new EOFException();            }            b[off+(n++)] = (byte)ch;        }    }    /**     * See the general contract of the <code>skipBytes</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @param      n   the number of bytes to be skipped.     * @return     the actual number of bytes skipped.     * @exception  IOException   if an I/O error occurs.     */    public final int skipBytes(int n) throws IOException {        return (skip(n));    }    /**     * See the general contract of the <code>readBoolean</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the <code>boolean</code> value read.     * @exception  EOFException  if this input stream has reached the end.     */    public final boolean readBoolean() throws IOException {        int ch = read();        if (ch < 0)            throw new EOFException();        return (ch != 0);    }    /**     * See the general contract of the <code>readByte</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next byte of this input stream as a signed 8-bit     *             <code>byte</code>.     * @exception  EOFException  if this input stream has reached the end.     * @exception  IOException   if an I/O error occurs.     */    public final byte readByte() throws IOException {        int ch = read();        if (ch < 0)            throw new EOFException();        return (byte)(ch);    }    /**     * See the general contract of the <code>readUnsignedByte</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next byte of this input stream, interpreted as an     *             unsigned 8-bit number.     * @exception  EOFException  if this input stream has reached the end.     * @exception  IOException   if an I/O error occurs.     */    public final int readUnsignedByte() throws IOException {        int ch = read();        if (ch < 0)            throw new EOFException();        return ch;    }    /**     * See the general contract of the <code>readShort</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next two bytes of this input stream, interpreted as a     *             signed 16-bit number.     * @exception  EOFException  if this input stream reaches the end before     *               reading two bytes.     * @exception  IOException   if an I/O error occurs.     */    public final short readShort() throws IOException {        int ch1 = read();        int ch2 = read();        if ((ch1 | ch2) < 0)             throw new EOFException();        return (short)((ch1 << 8) + (ch2 << 0));    }    /**     * See the general contract of the <code>readUnsignedShort</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next two bytes of this input stream, interpreted as an     *             unsigned 16-bit integer.     * @exception  EOFException  if this input stream reaches the end before     *               reading two bytes.     * @exception  IOException   if an I/O error occurs.     */    public final int readUnsignedShort() throws IOException {        int ch1 = read();        int ch2 = read();        if ((ch1 | ch2) < 0)             throw new EOFException();        return (ch1 << 8) + (ch2 << 0);    }    /**     * See the general contract of the <code>readChar</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next two bytes of this input stream as a Unicode     *             character.     * @exception  EOFException  if this input stream reaches the end before     *               reading two bytes.     * @exception  IOException   if an I/O error occurs.     */    public final char readChar() throws IOException {        int ch1 = read();        int ch2 = read();        if ((ch1 | ch2) < 0)             throw new EOFException();        return (char)((ch1 << 8) + (ch2 << 0));    }    /**     * See the general contract of the <code>readInt</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next four bytes of this input stream, interpreted as an     *             <code>int</code>.     * @exception  EOFException  if this input stream reaches the end before     *               reading four bytes.     * @exception  IOException   if an I/O error occurs.     */    public final int readInt() throws IOException {        int ch1 = read();        int ch2 = read();        int ch3 = read();        int ch4 = read();        if ((ch1 | ch2 | ch3 | ch4) < 0)             throw new EOFException();        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));    }    /**     * See the general contract of the <code>readLong</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     the next eight bytes of this input stream, interpreted as a     *             <code>long</code>.     * @exception  EOFException  if this input stream reaches the end before     *               reading eight bytes.     * @exception  IOException   if an I/O error occurs.     */    public final long readLong() throws IOException {        return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);    }    /**     * See the general contract of the <code>readUTF</code>     * method of <code>DataInput</code>.     * <p>     * Bytes     * for this operation are read from the contained     * input stream.     *     * @return     a Unicode string.     * @exception  EOFException  if this input stream reaches the end before     *               reading all the bytes.     * @exception  IOException   if an I/O error occurs.     * @see        java.io.DataInputStream#readUTF(java.io.DataInput)     */    public final String readUTF() throws IOException {        return DataInputStream.readUTF(this);    }    /**     * Unsupported function     */    public final float readFloat() throws IOException {        throw new RuntimeException("Function not supported");    }    /**     * Unsupported function     */    public final double readDouble() throws IOException {        throw new RuntimeException("Function not supported");    }    /**     * Unsupported function     */    public final String readLine() throws IOException {        throw new RuntimeException("Function not supported");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -