datagramobject.jpp
来自「This is a resource based on j2me embedde」· JPP 代码 · 共 824 行 · 第 1/2 页
JPP
824 行
public void writeInt(int v) throws IOException { write((v >>> 24) & 0xFF); write((v >>> 16) & 0xFF); write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); } /** * Writes a <code>long</code> to the underlying output stream as eight * bytes, high byte first. In no exception is thrown, the counter * <code>written</code> is incremented by <code>8</code>. * * @param v a <code>long</code> to be written. * @exception IOException if an I/O error occurs. */ public void writeLong(long v) throws IOException { write((int)(v >>> 56) & 0xFF); write((int)(v >>> 48) & 0xFF); write((int)(v >>> 40) & 0xFF); write((int)(v >>> 32) & 0xFF); write((int)(v >>> 24) & 0xFF); write((int)(v >>> 16) & 0xFF); write((int)(v >>> 8) & 0xFF); write((int)(v >>> 0) & 0xFF); } /** * Writes a string to the underlying output stream as a sequence of * characters. Each character is written to the data output stream as * if by the <code>writeChar</code> method. If no exception is * thrown, the counter <code>written</code> is incremented by twice * the length of <code>s</code>. * * @param s a <code>String</code> value to be written. * @exception IOException if an I/O error occurs. * @see java.io.DataOutputStream#writeChar(int) */ public void writeChars(String s) throws IOException { int len = s.length(); for (int i = 0 ; i < len ; i++) { int v = s.charAt(i); write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); } } /** * Writes a string to the underlying output stream using UTF-8 * encoding in a machine-independent manner. * <p> * First, two bytes are written to the output stream as if by the * <code>writeShort</code> method giving the number of bytes to * follow. This value is the number of bytes actually written out, * not the length of the string. Following the length, each character * of the string is output, in sequence, using the UTF-8 encoding * for the character. If no exception is thrown, the counter * <code>written</code> is incremented by the total number of * bytes written to the output stream. This will be at least two * plus the length of <code>str</code>, and at most two plus * thrice the length of <code>str</code>. * * @param str a string to be written. * @exception IOException if an I/O error occurs. */ public void writeUTF(String str) throws IOException { int strlen = str.length(); int utflen = 0; char[] charr = new char[strlen]; int c, count = 0; str.getChars(0, strlen, charr, 0); for (int i = 0; i < strlen; i++) { c = charr[i]; if ((c >= 0x0001) && (c <= 0x007F)) { utflen++; } else if (c > 0x07FF) { utflen += 3; } else { utflen += 2; } } if (utflen > 65535) throw new UTFDataFormatException(); byte[] bytearr = new byte[utflen+2]; bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); for (int i = 0; i < strlen; i++) { c = charr[i]; if ((c >= 0x0001) && (c <= 0x007F)) { bytearr[count++] = (byte) c; } else if (c > 0x07FF) { bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } write(bytearr); }//// DataInput methods// /** * 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 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 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 int skipBytes(int n) throws IOException { int total = 0; int cur = 0; while ((total<n) && ((cur = (int) skip(n-total)) > 0)) { total += cur; } return total; } /** * 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 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 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 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 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 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 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 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 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 String readUTF() throws IOException { return DataInputStream.readUTF(this); }// #ifdef ENABLE_CLDC_11 /** * See the general contract of the <code>writeFloat</code> * method of <code>DataInput</code>. * <p> * need revisit. * @param f float to write * @exception IOException if an I/O error occurs * @see java.io.DataInputStream#writeFloat(java.io.DataInput) */ public void writeFloat(float f) throws IOException { // Not implemented } /** * See the general contract of the <code>writeDouble</code> * method of <code>DataInput</code>. * <p> * need revisit. * @param d float to write * @exception IOException if an I/O error occurs * @see java.io.DataInputStream#writeDouble(java.io.DataInput) */ public void writeDouble(double d) throws IOException { // Not implemented } /** * See the general contract of the <code>readFloat</code> * method of <code>DataInput</code>. * <p> * need revisit: Always return 0.0. * @return float that was read * @exception IOException if an I/O error occurs * @see java.io.DataInputStream#readFloat(java.io.DataInput) */ public float readFloat() { // Not implemented return 0.0f; } /** * See the general contract of the <code>readDouble</code> * method of <code>DataInput</code>. * <p> * need revisit: Always return 0.0. * @return double read * @exception IOException if an I/O error occurs * @see java.io.DataInputStream#readDouble(java.io.DataInput) */ public double readDouble() { // Not implemented return 0.0; }// #endif ENABLE_CLDC_11}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?