universaloutputstream.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 573 行 · 第 1/2 页

JAVA
573
字号
/* * @(#)UniversalOutputStream.java	1.13 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package com.sun.cdc.io.j2me;import java.io.*;import com.sun.cdc.i18n.*;/** * This class is a combination of several J2SE stream classes. It implements * the OutputStream and DataOutput interfaces. It also implements the normal set of print() and * println() functions. Lastly the stream may, in certain cases, be * 'seeked' and have a timeout value associated with it. * <p> * Note: Printing a "\n" or calling println() will cause just a single line-feed character * to be placed in the output file. * * @version 1.0 1/7/2000 */public abstract class UniversalOutputStream extends OutputStream implements DataOutput {//// Binary output//    /**     * 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 a <code>float</code> to the underlying output stream as int     * bits. In no exception is thrown, the counter     * <code>written</code> is incremented by <code>8</code>.     *     * @param      v   a <code>float</code> to be written.     * @exception  IOException  if an I/O error occurs.     */    public void writeFloat(float v) throws IOException {        writeInt((int)(Float.floatToIntBits(v)));    }    public void writeDouble(double v) throws IOException {        writeLong(Double.doubleToLongBits(v));    }    /**     * 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.     */    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.     */    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;    }    /**     * writeBytes     */    public void writeBytes(String s) throws IOException {	byte strbyte[] = s.getBytes();        for (int c=0; c < strbyte.length; c++) {	    writeByte((int)strbyte[c]);	}    }//// Text output//    /**     * The writer used for printing output text     */    private Writer writer;    /**     * An error flag that can be enquired     */    private boolean trouble = false;    /**     * A flag to prevent infinitly recursive flushing     */    private boolean flushing = false;    /**     * Set the encoding for text output. All the print() routines

⌨️ 快捷键说明

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