📄 generalbase.java
字号:
/* * Copyright (c) 1999-2001 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;import java.io.*;import javax.microedition.io.*;/** * This is the base class for objects that implement the DataInput * and DataOutput interfaces. The class also behaves like a * DataOutputStream so it can be used for UniversalOutputStream * * @author Nik Shaylor * @version 1.1 2/20/2000 */abstract public class GeneralBase implements DataInput, DataOutput { /** * Writes the specified byte (the low eight bits of the argument * <code>b</code>) to the underlying output stream. If no exception * is thrown, the counter <code>written</code> is incremented by * <code>1</code>. * <p> * Implements the <code>write</code> method of <code>OutputStream</code>. * * @param b the <code>byte</code> to be written. * @exception IOException if an I/O error occurs. */ public void write(int b) throws IOException { throw new RuntimeException("No write()"); } /** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to the underlying output stream. * If no exception is thrown, the counter <code>written</code> is * incremented by <code>len</code>. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException if an I/O error occurs. */ public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } /** * Writes <code>len</code> bytes from the specified byte array. * * @param b the data. * @exception IOException if an I/O error occurs. */ public final void write(byte b[]) throws IOException { write(b, 0, b.length); } /** * Writes a <code>boolean</code> to the underlying output stream as * a 1-byte value. The value <code>true</code> is written out as the * value <code>(byte)1</code>; the value <code>false</code> is * written out as the value <code>(byte)0</code>. If no exception is * thrown, the counter <code>written</code> is incremented by * <code>1</code>. * * @param v a <code>boolean</code> value to be written. * @exception IOException if an I/O error occurs. */ public void writeBoolean(boolean v) throws IOException { write(v ? 1 : 0); } /** * Writes out a <code>byte</code> to the underlying output stream as * a 1-byte value. If no exception is thrown, the counter * <code>written</code> is incremented by <code>1</code>. * * @param v a <code>byte</code> value to be written. * @exception IOException if an I/O error occurs. */ public void writeByte(int v) throws IOException { write(v); } /** * Writes a <code>short</code> to the underlying output stream as two * bytes, high byte first. If no exception is thrown, the counter * <code>written</code> is incremented by <code>2</code>. * * @param v a <code>short</code> to be written. * @exception IOException if an I/O error occurs. */ public void writeShort(int v) throws IOException { write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); } /** * Writes a <code>char</code> to the underlying output stream as a * 2-byte value, high byte first. If no exception is thrown, the * counter <code>written</code> is incremented by <code>2</code>. * * @param v a <code>char</code> value to be written. * @exception IOException if an I/O error occurs. */ public void writeChar(int v) throws IOException { write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); } /** * Writes an <code>int</code> to the underlying output stream as four * bytes, high byte first. If no exception is thrown, the counter * <code>written</code> is incremented by <code>4</code>. * * @param v an <code>int</code> to be written. * @exception IOException if an I/O error occurs. */ 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 out the string to the underlying output stream as a * sequence of bytes. Each character in the string is written out, in * sequence, by discarding its high eight bits. If no exception is * thrown, the counter <code>written</code> is incremented by the * length of <code>s</code>. * * @param s a string of bytes to be written. * @exception IOException if an I/O error occurs. */ public void writeBytes(String s) throws IOException { throw new RuntimeException("Function not supported"); } /** * 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 { writeUTF(str, this); } /** * Writes a string to the specified DataOutput using UTF-8 encoding in a * machine-independent manner. * <p> * First, two bytes are written to out 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. * @param out destination to write to * @return The number of bytes written out. * @exception IOException if an I/O error occurs. */ public static int writeUTF(String str, DataOutput out) 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)); } } out.write(bytearr); return utflen + 2; } /** * Flushes this data output stream. This forces any buffered output * bytes to be written out to the stream. * <p> * The <code>flush</code> method of <code>DataOutputStream</code> * calls the <code>flush</code> method of its underlying output stream. * * @exception IOException if an I/O error occurs. */ public void flush() throws IOException { } /** * Closes this output stream and releases any system resources * associated with the stream. * <p> * The <code>close</code> method * calls its <code>flush</code> method, and then calls the * <code>close</code> method of its underlying output stream. * * @exception IOException if an I/O error occurs.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -