/* * @(#)GeneralBase.java 1.12 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; import java.io.*; import javax.microedition.io.*; /** * Base class for objects that implement the DataInput and DataOutput interfaces. * It also behaves like a DataOutputStream so it can be used for UniversalOutputStream * * @version 1.1 2/20/2000 */ abstract public class GeneralBase extends DataOutputStream implements DataInput, DataOutput { public GeneralBase() { super(null); // No DataOutputStream redirection wanted } /** * 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. * @see java.io.FilterOutputStream#out */ 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. * @see java.io.FilterOutputStream#out */ 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]); } } // // DataOutput functions are inherited from DataOutputStream // /** * Flush the stream. */ public void flush() throws IOException { } /** * Close the stream. */ public void close() throws IOException { } // // DataInput functions // /** * Reads the next byte of data from the input stream. The value byte is * returned as an <code>int</code> in the range <code>0</code> to * <code>255</code>. If no byte is available because the end of the stream * has been reached, the value <code>-1</code> is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown. * * <p> A subclass must provide an implementation of this method. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */ public int read() throws IOException { throw new RuntimeException("No read()"); } /** * Skips over and discards <code>n</code> bytes of data from this input * stream. The <code>skip</code> method may, for a variety of reasons, end * up skipping over some smaller number of bytes, possibly <code>0</code>. * This may result from any of a number of conditions; reaching end of file * before <code>n</code> bytes have been skipped is only one possibility. * The actual number of bytes skipped is returned. If <code>n</code> is * negative, no bytes are skipped. * * <p> The <code>skip</code> method of <code>InputStream</code> creates a * byte array and then repeatedly reads into it until <code>n</code> bytes * have been read or the end of the stream has been reached. Subclasses are * encouraged to provide a more efficient implementation of this method. * * @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 long skip(long n) throws IOException { long m = n; while(m > 0) { if(read() > 0) { break; } --m; } return n-m; } /** * 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); } /** * Unsupported function */ public float readFloat() throws IOException { throw new RuntimeException("Function not supported"); } /** * Unsupported function */ public double readDouble() throws IOException { throw new RuntimeException("Function not supported"); } /** * Unsupported function */ public String readLine() throws IOException { throw new RuntimeException("Function not supported"); } }