You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ge...@apache.org on 2005/12/01 07:04:00 UTC
svn commit: r350181 [93/198] - in
/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core: ./ depends/
depends/files/ depends/jars/ depends/libs/ depends/libs/linux.IA32/
depends/libs/win.IA32/ depends/oss/ depends/oss/linux.IA32/
depends/oss/win.I...
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/BufferedWriter.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/BufferedWriter.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/BufferedWriter.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/BufferedWriter.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,286 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+import java.security.AccessController;
+
+import com.ibm.oti.util.PriviAction;
+
+/**
+ * BufferedWriter is for writing buffered character output. Characters written
+ * to this Writer are buffered internally before being commited to the target
+ * Writer.
+ *
+ * @see BufferedReader
+ */
+public class BufferedWriter extends Writer {
+ private Writer out;
+
+ private char buf[];
+
+ private int pos;
+
+ private final String lineSeparator = (String) AccessController
+ .doPrivileged(new PriviAction("line.separator")); //$NON-NLS-1$
+
+ /**
+ * Constructs a new BufferedReader with <code>out</code> as the Writer on
+ * which to buffer write operations. The buffer size is set to the default,
+ * which is 8K.
+ *
+ * @param out
+ * The Writer to buffer character writing on
+ */
+
+ public BufferedWriter(Writer out) {
+ super(out);
+ this.out = out;
+ buf = new char[8192];
+ }
+
+ /**
+ * Constructs a new BufferedReader with <code>out</code> as the Writer on
+ * which buffer write operations. The buffer size is set to
+ * <code>size</code>.
+ *
+ * @param out
+ * The Writer to buffer character writing on.
+ * @param size
+ * The size of the buffer to use.
+ */
+
+ public BufferedWriter(Writer out, int size) {
+ super(out);
+ if (size > 0) {
+ this.out = out;
+ this.buf = new char[size];
+ } else
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg
+ .getString("K0058")); //$NON-NLS-1$
+ }
+
+ /**
+ * Close this BufferedWriter. The contents of the buffer are flushed, the
+ * target writer is closed, and the buffer is released. Only the first
+ * invocation of close has any effect.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this Writer.
+ */
+
+ public void close() throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ flush();
+ out.close();
+ buf = null;
+ out = null;
+ }
+ }
+ }
+
+ /**
+ * Flush this BufferedWriter. The contents of the buffer are committed to
+ * the target writer and it is then flushed.
+ *
+ * @throws IOException
+ * If an error occurs attempting to flush this Writer.
+ */
+
+ public void flush() throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos > 0)
+ out.write(buf, 0, pos);
+ pos = 0;
+ out.flush();
+ } else
+ throw new IOException(com.ibm.oti.util.Msg.getString("K005d")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Answer a boolean indicating whether or not this BufferedWriter is open.
+ *
+ * @return <code>true</code> if this reader is open, <code>false</code>
+ * otherwise
+ */
+ private boolean isOpen() {
+ return out != null;
+ }
+
+ /**
+ * Write a newline to thie Writer. A newline is determined by the System
+ * property "line.separator". The target writer may or may not be flushed
+ * when a newline is written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this Writer.
+ */
+
+ public void newLine() throws IOException {
+ write(lineSeparator, 0, lineSeparator.length());
+ }
+
+ /**
+ * Writes out <code>count</code> characters starting at
+ * <code>offset</code> in <code>buf</code> to this BufferedWriter. If
+ * <code>count</code> is greater than this Writers buffer then flush the
+ * contents and also write the characters directly to the target Writer.
+ *
+ * @param cbuf
+ * the non-null array containing characters to write.
+ * @param offset
+ * offset in buf to retrieve characters
+ * @param count
+ * maximum number of characters to write
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ * @throws ArrayIndexOutOfBoundsException
+ * If offset or count are outside of bounds.
+ */
+
+ public void write(char[] cbuf, int offset, int count) throws IOException {
+ // avoid int overflow
+ if (0 <= offset && offset <= cbuf.length && 0 <= count
+ && count <= cbuf.length - offset) {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos == 0 && count >= this.buf.length) {
+ out.write(cbuf, offset, count);
+ return;
+ }
+ int available = this.buf.length - pos;
+ if (count < available)
+ available = count;
+ if (available > 0) {
+ System
+ .arraycopy(cbuf, offset, this.buf, pos,
+ available);
+ pos += available;
+ }
+ if (pos == this.buf.length) {
+ out.write(this.buf, 0, this.buf.length);
+ pos = 0;
+ if (count > available) {
+ offset += available;
+ available = count - available;
+ if (available >= this.buf.length) {
+ out.write(cbuf, offset, available);
+ return;
+ }
+ System.arraycopy(cbuf, offset, this.buf, pos,
+ available);
+ pos += available;
+ }
+ }
+ } else
+ throw new IOException(com.ibm.oti.util.Msg
+ .getString("K005d")); //$NON-NLS-1$
+ }
+ } else
+ throw new ArrayIndexOutOfBoundsException();
+ }
+
+ /**
+ * Writes the character
+ * <code>oneChar<code> BufferedWriter. If the buffer is filled by
+ * writing this character, flush this Writer. Only the lower 2 bytes are written.
+ *
+ * @param oneChar The Character to write out.
+ *
+ * @throws IOException If this Writer has already been closed or some other IOException occurs.
+ */
+ public void write(int oneChar) throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos >= buf.length) {
+ out.write(buf, 0, buf.length);
+ pos = 0;
+ }
+ buf[pos++] = (char) oneChar;
+ } else
+ throw new IOException(com.ibm.oti.util.Msg.getString("K005d")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Writes out <code>count</code> characters starting at
+ * <code>offset</code> in <code>str</code> to this BufferedWriter. If
+ * <code>count</code> is greater than this Writers buffer then flush the
+ * contents and also write the characters directly to the target Writer.
+ *
+ * @param str
+ * the non-null String containing characters to write
+ * @param offset
+ * offset in str to retrieve characters
+ * @param count
+ * maximum number of characters to write
+ *
+ * @throws IOException
+ * If this Writer has already been closed or some other
+ * IOException occurs.
+ * @throws ArrayIndexOutOfBoundsException
+ * If offset or count are outside of bounds.
+ */
+
+ public void write(String str, int offset, int count) throws IOException {
+ // avoid int overflow
+ if (0 <= offset && offset <= str.length() && 0 <= count
+ && count <= str.length() - offset) {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos == 0 && count >= buf.length) {
+ char[] chars = new char[count];
+ str.getChars(offset, offset + count, chars, 0);
+ out.write(chars, 0, count);
+ return;
+ }
+ int available = buf.length - pos;
+ if (count < available)
+ available = count;
+ if (available > 0) {
+ str.getChars(offset, offset + available, buf, pos);
+ pos += available;
+ }
+ if (pos == buf.length) {
+ out.write(this.buf, 0, this.buf.length);
+ pos = 0;
+ if (count > available) {
+ offset += available;
+ available = count - available;
+ if (available >= buf.length) {
+ char[] chars = new char[count];
+ str.getChars(offset, offset + available, chars,
+ 0);
+ out.write(chars, 0, available);
+ return;
+ }
+ str.getChars(offset, offset + available, buf, pos);
+ pos += available;
+ }
+ }
+ } else
+ throw new IOException(com.ibm.oti.util.Msg
+ .getString("K005d")); //$NON-NLS-1$
+ }
+ } else
+ throw new StringIndexOutOfBoundsException();
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayInputStream.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayInputStream.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayInputStream.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,203 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * ByteArrayInputStream is used for streaming over a byte array.
+ *
+ * @see ByteArrayOutputStream
+ */
+public class ByteArrayInputStream extends InputStream {
+ /**
+ * The <code>byte</code> array containing the bytes to stream over.
+ */
+ protected byte[] buf;
+
+ /**
+ * The current position within the byte array.
+ */
+ protected int pos;
+
+ /**
+ * The current mark position. Initially set to 0 or the <code>offset</code>
+ * parameter within the constructor.
+ */
+ protected int mark;
+
+ /**
+ * The total number of bytes initially available in the byte array
+ * <code>buf</code>.
+ */
+ protected int count;
+
+ /**
+ * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>.
+ *
+ * @param buf
+ * the byte array to stream over
+ */
+ public ByteArrayInputStream(byte buf[]) {
+ this.mark = 0;
+ this.buf = buf;
+ this.count = buf.length;
+ }
+
+ /**
+ * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>
+ * with the position set to <code>offset</code> and the number of bytes
+ * available set to <code>offset</code> + <code>length</code>.
+ *
+ * @param buf
+ * the byte array to stream over
+ * @param offset
+ * the offset in <code>buf</code> to start streaming at
+ * @param length
+ * the number of bytes available to stream over.
+ */
+ public ByteArrayInputStream(byte buf[], int offset, int length) {
+ this.buf = buf;
+ pos = offset >= buf.length ? buf.length : offset;
+ mark = pos;
+ count = length + pos > buf.length ? buf.length : length + pos;
+ }
+
+ /**
+ * Answers a int representing then number of bytes that are available before
+ * this ByteArrayInputStream will block. This method returns the number of
+ * bytes yet to be read from the underlying byte array.
+ *
+ * @return the number of bytes available before blocking.
+ */
+ public synchronized int available() {
+ return count - pos;
+ }
+
+ /**
+ * Close the ByteArrayInputStream. This implementation frees up resources
+ * associated with this stream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this InputStream.
+ */
+ public void close() throws IOException {
+ // Do nothing on close, this matches JDK behaviour.
+ }
+
+ /**
+ * Set a Mark position in this ByteArrayInputStream. The parameter
+ * <code>readLimit</code> is ignored. Sending reset() will reposition the
+ * stream back to the marked position.
+ *
+ * @param readlimit
+ * ignored.
+ */
+ public void mark(int readlimit) {
+ mark = pos;
+ }
+
+ /**
+ * Answers a boolean indicating whether or not this ByteArrayInputStream
+ * supports mark() and reset(). This implementation answers
+ * <code>true</code>.
+ *
+ * @return <code>true</code> indicates this stream supports mark/reset,
+ * <code>false
+ * <code> otherwise.
+ */
+ public boolean markSupported() {
+ return true;
+ }
+
+ /**
+ * Reads a single byte from this ByteArrayInputStream and returns the result
+ * as an int. The low-order byte is returned or -1 of the end of stream was
+ * encountered. This implementation returns the next available byte from the
+ * target byte array.
+ *
+ * @return the byte read or -1 if end of stream.
+ */
+ public synchronized int read() {
+ return pos < count ? buf[pos++] & 0xFF : -1;
+ }
+
+ /**
+ * Reads at most <code>len</code> bytes from this ByteArrayInputStream and
+ * stores them in byte array <code>b</code> starting at offset
+ * <code>off</code>. Answer the number of bytes actually read or -1 if no
+ * bytes were read and end of stream was encountered. This implementation
+ * reads bytes from the target byte array.
+ *
+ * @param b
+ * the byte array in which to store the read bytes.
+ * @param offset
+ * the offset in <code>b</code> to store the read bytes.
+ * @param length
+ * the maximum number of bytes to store in <code>b</code>.
+ * @return the number of bytes actually read or -1 if end of stream.
+ */
+ public synchronized int read(byte b[], int offset, int length) {
+ // Are there any bytes available
+ if (this.pos >= this.count)
+ return -1;
+
+ if (b != null) {
+ // avoid int overflow
+ if (0 <= offset && offset <= b.length && 0 <= length
+ && length <= b.length - offset) {
+ if (length == 0)
+ return 0;
+
+ int copylen = this.count - pos < length ? this.count - pos
+ : length;
+ System.arraycopy(buf, pos, b, offset, copylen);
+ pos += copylen;
+ return copylen;
+ }
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ throw new NullPointerException();
+ }
+
+ /**
+ * Reset this ByteArrayInputStream to the last marked location. This
+ * implementation resets the position to either the marked position, the
+ * start position supplied in the constructor or <code>0</code> if neither
+ * is provided.
+ *
+ */
+ public synchronized void reset() {
+ pos = mark;
+ }
+
+ /**
+ * Skips <code>count</code> number of bytes in this InputStream.
+ * Subsequent <code>read()</code>'s will not return these bytes unless
+ * <code>reset()</code> is used. This implementation skips
+ * <code>count</code> number of bytes in the target stream.
+ *
+ * @param n
+ * the number of bytes to skip.
+ * @return the number of bytes actually skipped.
+ */
+ public synchronized long skip(long n) {
+ if (n <= 0)
+ return 0;
+ int temp = pos;
+ pos = this.count - pos < n ? this.count : (int) (pos + n);
+ return pos - temp;
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayOutputStream.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayOutputStream.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/ByteArrayOutputStream.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,245 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * ByteArrayOutputStream is a class whose underlying stream is represented by a
+ * byte array. As bytes are written to this stream, the local byte array may be
+ * expanded to hold more bytes.
+ *
+ * @see ByteArrayInputStream
+ */
+public class ByteArrayOutputStream extends OutputStream {
+ /**
+ * The byte array containing the bytes written.
+ */
+ protected byte[] buf;
+
+ /**
+ * The number of bytes written.
+ */
+ protected int count;
+
+ /**
+ * Constructs a new ByteArrayOutputStream with a default size of 32 bytes.
+ * If more than 32 bytes are written to this instance, the underlying byte
+ * array will expand to accomodate.
+ *
+ */
+ public ByteArrayOutputStream() {
+ super();
+ buf = new byte[32];
+ }
+
+ /**
+ * Constructs a new ByteArrayOutputStream with a default size of
+ * <code>size</code> bytes. If more than <code>size</code> bytes are
+ * written to this instance, the underlying byte array will expand to
+ * accomodate.
+ *
+ * @param size
+ * an non-negative integer representing the initial size for the
+ * underlying byte array.
+ */
+ public ByteArrayOutputStream(int size) {
+ super();
+ if (size >= 0)
+ buf = new byte[size];
+ else
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg
+ .getString("K005e")); //$NON-NLS-1$
+ }
+
+ /**
+ * Close this ByteArrayOutputStream. This implementation releases System
+ * resources used for this stream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this OutputStream.
+ */
+ public void close() throws IOException {
+ /**
+ * Although the spec claims "A closed stream cannot perform output
+ * operations and cannot be reopened.", this implementation must do
+ * nothing.
+ */
+ super.close();
+ }
+
+ private void expand(int i) {
+ /* Can the buffer handle @i more bytes, if not expand it */
+ if (count + i <= buf.length)
+ return;
+
+ byte[] newbuf = new byte[(count + i) * 2];
+ System.arraycopy(buf, 0, newbuf, 0, count);
+ buf = newbuf;
+ }
+
+ /**
+ * Reset this ByteArrayOutputStream to the beginning of the underlying byte
+ * array. All subsequent writes will overwrite any bytes previously stored
+ * in this stream.
+ *
+ */
+ public synchronized void reset() {
+ count = 0;
+ }
+
+ /**
+ * Answers the total number of bytes written to this stream thus far.
+ *
+ * @return the number of bytes written to this Stream.
+ */
+ public int size() {
+ return count;
+ }
+
+ /**
+ * Answer the contents of this ByteArrayOutputStream as a byte array. Any
+ * changes made to the receiver after returning will not be reflected in the
+ * byte array returned to the caller.
+ *
+ * @return this streams current contents as a byte array.
+ */
+ public synchronized byte[] toByteArray() {
+ byte[] newArray = new byte[count];
+ System.arraycopy(buf, 0, newArray, 0, count);
+ return newArray;
+ }
+
+ /**
+ * Answer the contents of this ByteArrayOutputStream as a String. Any
+ * changes made to the receiver after returning will not be reflected in the
+ * String returned to the caller.
+ *
+ * @return this streams current contents as a String.
+ */
+
+ public String toString() {
+ return new String(buf, 0, count);
+ }
+
+ /**
+ * Answer the contents of this ByteArrayOutputStream as a String. Each byte
+ * <code>b</code> in this stream is converted to a character
+ * <code>c</code> using the following function:
+ * <code>c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))</code>. This
+ * method is deprecated and either toString(), or toString(enc) should be
+ * used.
+ *
+ * @param hibyte
+ * the high byte of each resulting Unicode character
+ * @return this streams current contents as a String with the high byte set
+ * to <code>hibyte</code>
+ *
+ * @deprecated Use toString()
+ */
+ public String toString(int hibyte) {
+ char[] newBuf = new char[size()];
+ for (int i = 0; i < newBuf.length; i++)
+ newBuf[i] = (char) (((hibyte & 0xff) << 8) | (buf[i] & 0xff));
+ return new String(newBuf);
+ }
+
+ /**
+ * Answer the contents of this ByteArrayOutputStream as a String converted
+ * using the encoding declared in <code>enc</code>.
+ *
+ * @param enc
+ * A String representing the encoding to use when translating
+ * this stream to a String.
+ * @return this streams current contents as a String.
+ *
+ * @throws UnsupportedEncodingException
+ * If declared encoding is not supported
+ */
+ public String toString(String enc) throws UnsupportedEncodingException {
+ return new String(buf, 0, count, enc);
+ }
+
+ /**
+ * Writes <code>count</code> <code>bytes</code> from the byte array
+ * <code>buffer</code> starting at offset <code>index</code> to the
+ * ByteArrayOutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ * @param offset
+ * offset in buffer to get bytes
+ * @param len
+ * number of bytes in buffer to write
+ *
+ * @throws NullPointerException
+ * If buffer is null.
+ * @throws IndexOutOfBoundsException
+ * If offset or count are outside of bounds.
+ */
+ public synchronized void write(byte[] buffer, int offset, int len) {
+ /* Unsure what to do here, spec is unclear */
+ if (buf == null)
+ return;
+ if (buffer != null) {
+ // avoid int overflow
+ if (0 <= offset && offset <= buffer.length && 0 <= len
+ && len <= buffer.length - offset) {
+ /* Expand if necessary */
+ expand(len);
+ System.arraycopy(buffer, offset, buf, this.count, len);
+ this.count += len;
+ } else
+ throw new ArrayIndexOutOfBoundsException(com.ibm.oti.util.Msg
+ .getString("K002f")); //$NON-NLS-1$
+ } else
+ throw new NullPointerException(com.ibm.oti.util.Msg
+ .getString("K0047")); //$NON-NLS-1$
+ }
+
+ /**
+ * Writes the specified byte <code>oneByte</code> to the OutputStream.
+ * Only the low order byte of <code>oneByte</code> is written.
+ *
+ * @param oneByte
+ * the byte to be written
+ */
+ public synchronized void write(int oneByte) {
+ try {
+ buf[count] = (byte) oneByte;
+ count++;
+ } catch (IndexOutOfBoundsException e) {
+ // Expand when necessary
+ expand(1);
+ buf[count++] = (byte) oneByte;
+ } catch (NullPointerException e) {
+ }
+ }
+
+ /**
+ * Take the contents of this stream and write it to the output stream
+ * <code>out</code>.
+ *
+ * @param out
+ * An OutputStream on which to write the contents of this stream.
+ *
+ * @throws IOException
+ * If an error occurs when writing to output stream
+ */
+ public void writeTo(OutputStream out) throws IOException {
+ out.write(buf, 0, count);
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayReader.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayReader.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayReader.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayReader.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,272 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * CharArrayReader is used as a buffered character input stream on a character
+ * array.
+ *
+ */
+public class CharArrayReader extends Reader {
+ /**
+ * Buffer for characters
+ */
+ protected char buf[];
+
+ /**
+ * Current buffer position.
+ */
+ protected int pos = 0;
+
+ /**
+ * Current mark position.
+ */
+ protected int markedPos = -1;
+
+ /**
+ * The ending index of the buffer.
+ */
+ protected int count = 0;
+
+ /**
+ * Construct a CharArrayReader on the char array <code>buffer</code>. The
+ * size of the reader is set to the <code>length()</code> of the buffer
+ * and the Object to synchronize access through is set to
+ * <code>buffer</code>.
+ *
+ * @param buf
+ * the char array to filter reads on.
+ */
+ public CharArrayReader(char[] buf) {
+ super(buf);
+ this.buf = buf;
+ this.count = buf.length;
+ }
+
+ /**
+ * Construct a CharArrayReader on the char array <code>buffer</code>. The
+ * size of the reader is set to the parameter <code>length()</code> and
+ * the original offset is set to <code>offset</code>.
+ *
+ * @param buf
+ * the char array to filter reads on.
+ * @param offset
+ * the offset in <code>buf</code> to start streaming at.
+ * @param length
+ * the number of characters available to stream over.
+ */
+
+ public CharArrayReader(char[] buf, int offset, int length) {
+ super(buf);
+ if (0 <= offset && offset <= buf.length && length >= 0) {
+ this.buf = buf;
+ this.pos = offset;
+
+ /* This is according to spec */
+ this.count = this.pos + length < buf.length ? length : buf.length;
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * This method closes this CharArrayReader. Once it is closed, you can no
+ * longer read from it. Only the first invocation of this method has any
+ * effect.
+ *
+ */
+ public void close() {
+ synchronized (lock) {
+ if (isOpen())
+ buf = null;
+ }
+ }
+
+ /**
+ * Answer a boolean indicating whether or not this CharArrayReader is open.
+ *
+ * @return <code>true</code> if the reader is open, <code>false</code>
+ * otherwise.
+ */
+ private boolean isOpen() {
+ return buf != null;
+ }
+
+ /**
+ * Set a Mark position in this Reader. The parameter <code>readLimit</code>
+ * is ignored for CharArrayReaders. Sending reset() will reposition the
+ * reader back to the marked position provided the mark has not been
+ * invalidated.
+ * @param readLimit
+ * ignored for CharArrayReaders.
+ *
+ * @throws IOException
+ * If an error occurs attempting to mark this CharArrayReader.
+ */
+ public void mark(int readLimit) throws IOException {
+ synchronized (lock) {
+ if (isOpen())
+ markedPos = pos;
+ else
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Answers a boolean indicating whether or not this CharArrayReader supports
+ * mark() and reset(). This method always returns true.
+ *
+ * @return indicates whether or not mark() and reset() are supported.
+ */
+ public boolean markSupported() {
+ return true;
+ }
+
+ /**
+ * Reads a single character from this CharArrayReader and returns the result
+ * as an int. The 2 higher-order bytes are set to 0. If the end of reader
+ * was encountered then return -1.
+ *
+ * @return int the character read or -1 if end of reader.
+ *
+ * @throws IOException
+ * If the CharArrayReader is already closed.
+ */
+ public int read() throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos != count)
+ return buf[pos++];
+ return -1;
+ }
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Reads at most <code>count</code> characters from this CharArrayReader
+ * and stores them at <code>offset</code> in the character array
+ * <code>buf</code>. Returns the number of characters actually read or -1
+ * if the end of reader was encountered.
+ *
+ *
+ * @param buffer
+ * character array to store the read characters
+ * @param offset
+ * offset in buf to store the read characters
+ * @param len
+ * maximum number of characters to read
+ * @return number of characters read or -1 if end of reader.
+ *
+ * @throws IOException
+ * If the CharArrayReader is closed.
+ */
+
+ public int read(char buffer[], int offset, int len) throws IOException {
+ // avoid int overflow
+ if (0 <= offset && offset <= buffer.length && 0 <= len
+ && len <= buffer.length - offset) {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (pos != this.count) {
+ int bytesRead = pos + len > this.count ? this.count
+ - pos : len;
+ System.arraycopy(this.buf, pos, buffer, offset,
+ bytesRead);
+ pos += bytesRead;
+ return bytesRead;
+ }
+ return -1;
+ }
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+ throw new ArrayIndexOutOfBoundsException();
+ }
+
+ /**
+ * Answers a <code>boolean</code> indicating whether or not this
+ * CharArrayReader is ready to be read without blocking. If the result is
+ * <code>true</code>, the next <code>read()</code> will not block. If
+ * the result is <code>false</code> this Reader may or may not block when
+ * <code>read()</code> is sent. The implementation in CharArrayReader
+ * always returns <code>true</code> even when it has been closed.
+ *
+ * @return <code>true</code> if the receiver will not block when
+ * <code>read()</code> is called, <code>false</code> if unknown
+ * or blocking will occur.
+ *
+ * @throws IOException
+ * If the CharArrayReader is closed.
+ */
+ public boolean ready() throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ return pos != count;
+ }
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Reset this CharArrayReader's position to the last <code>mark()</code>
+ * location. Invocations of <code>read()/skip()</code> will occur from
+ * this new location. If this Reader was not marked, the CharArrayReader is
+ * reset to the beginning of the String.
+ *
+ * @throws IOException
+ * If this CharArrayReader has already been closed.
+ */
+ public void reset() throws IOException {
+ synchronized (lock) {
+ if (isOpen())
+ pos = markedPos != -1 ? markedPos : 0;
+ else
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Skips <code>count</code> number of characters in this CharArrayReader.
+ * Subsequent <code>read()</code>'s will not return these characters
+ * unless <code>reset()</code> is used.
+ *
+ * @param n
+ * The number of characters to skip.
+ * @return long The number of characters actually skipped.
+ *
+ * @throws IOException
+ * If this CharArrayReader has already been closed.
+ */
+ public long skip(long n) throws IOException {
+ synchronized (lock) {
+ if (isOpen()) {
+ if (n <= 0)
+ return 0;
+ long skipped = 0;
+ if (n < this.count - pos) {
+ pos = pos + (int) n;
+ skipped = n;
+ } else {
+ skipped = this.count - pos;
+ pos = this.count;
+ }
+ return skipped;
+ }
+ throw new IOException(com.ibm.oti.util.Msg.getString("K0060")); //$NON-NLS-1$
+ }
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayWriter.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayWriter.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayWriter.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharArrayWriter.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,227 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * CharArrayWriter is used as a character output stream on a character array.
+ * The buffer used to store the written characters will grow as needed to
+ * accomodate more characters as they are written.
+ *
+ */
+public class CharArrayWriter extends Writer {
+ /**
+ * Buffer for characters
+ */
+ protected char[] buf;
+
+ /**
+ * The ending index of the buffer.
+ */
+ protected int count;
+
+ /**
+ * Constructs a new CharArrayWriter which has a buffer allocated with the
+ * default size of 32 characters. The buffer is also the <code>lock</code>
+ * used to synchronize access to this Writer.
+ *
+ */
+ public CharArrayWriter() {
+ super();
+ buf = new char[32];
+ lock = buf;
+ }
+
+ /**
+ * Constructs a new CharArrayWriter which has a buffer allocated with the
+ * size of <code>initialSize</code> characters. The buffer is also the
+ * <code>lock</code> used to synchronize access to this Writer.
+ *
+ * @param initialSize
+ * the initial size of this CharArrayWriters buffer.
+ */
+ public CharArrayWriter(int initialSize) {
+ super();
+ if (initialSize >= 0) {
+ buf = new char[initialSize];
+ lock = buf;
+ } else
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg
+ .getString("K005e")); //$NON-NLS-1$
+ }
+
+ /**
+ * Close this Writer. This is the concrete implementation required. This
+ * particular implementation does nothing.
+ *
+ */
+ public void close() {
+ /* empty */
+ }
+
+ private void expand(int i) {
+ /* Can the buffer handle @i more chars, if not expand it */
+ if (count + i <= buf.length)
+ return;
+
+ char[] newbuf = new char[buf.length + (2 * i)];
+ System.arraycopy(buf, 0, newbuf, 0, count);
+ buf = newbuf;
+ }
+
+ /**
+ * Flush this Writer. This is the concrete implementation required. This
+ * particular implementation does nothing.
+ *
+ */
+
+ public void flush() {
+ /* empty */
+ }
+
+ /**
+ * Reset this Writer. The current write position is reset to the beginning
+ * of the buffer. All written characters are lost and the size of this
+ * writer is now 0.
+ *
+ */
+ public void reset() {
+ synchronized (lock) {
+ count = 0;
+ }
+ }
+
+ /**
+ * Answer the size of this Writer in characters. This number changes if this
+ * Writer is reset or as more characters are written to it.
+ *
+ * @return int this CharArrayWriters current size in characters.
+ */
+ public int size() {
+ synchronized (lock) {
+ return count;
+ }
+ }
+
+ /**
+ * Answer the contents of the receiver as a char array. The array returned
+ * is a copy and any modifications made to this Writer after are not
+ * reflected in the result.
+ *
+ * @return char[] this CharArrayWriters contents as a new char array.
+ */
+ public char[] toCharArray() {
+ synchronized (lock) {
+ char[] result = new char[count];
+ System.arraycopy(buf, 0, result, 0, count);
+ return result;
+ }
+ }
+
+ /**
+ * Answer the contents of this CharArrayWriter as a String. The String
+ * returned is a copy and any modifications made to this Writer after are
+ * not reflected in the result.
+ *
+ * @return String this CharArrayWriters contents as a new String.
+ */
+ public String toString() {
+ synchronized (lock) {
+ return new String(buf, 0, count);
+ }
+ }
+
+ /**
+ * Writes <code>count</code> characters starting at <code>offset</code>
+ * in <code>buf</code> to this CharArrayWriter.
+ *
+ * @param c
+ * the non-null array containing characters to write.
+ * @param offset
+ * offset in buf to retrieve characters
+ * @param len
+ * maximum number of characters to write
+ */
+ public void write(char[] c, int offset, int len) {
+ // avoid int overflow
+ if (0 <= offset && offset <= c.length && 0 <= len
+ && len <= c.length - offset) {
+ synchronized (lock) {
+ expand(len);
+ System.arraycopy(c, offset, this.buf, this.count, len);
+ this.count += len;
+ }
+ } else
+ throw new ArrayIndexOutOfBoundsException();
+ }
+
+ /**
+ * Writes the specified character <code>oneChar</code> to this
+ * CharArrayWriter. This implementation writes the low order two bytes to
+ * the Stream.
+ *
+ * @param oneChar
+ * The character to write
+ */
+ public void write(int oneChar) {
+ synchronized (lock) {
+ expand(1);
+ buf[count++] = (char) oneChar;
+ }
+ }
+
+ /**
+ * Writes <code>count</code> number of characters starting at
+ * <code>offset</code> from the String <code>str</code> to this
+ * CharArrayWriter.
+ *
+ * @param str
+ * the non-null String containing the characters to write.
+ * @param offset
+ * the starting point to retrieve characters.
+ * @param len
+ * the number of characters to retrieve and write.
+ */
+ public void write(String str, int offset, int len) {
+ // avoid int overflow
+ if (0 <= offset && offset <= str.length() && 0 <= len
+ && len <= str.length() - offset) {
+ synchronized (lock) {
+ expand(len);
+ str.getChars(offset, offset + len, buf, this.count);
+ this.count += len;
+ }
+ } else
+ throw new StringIndexOutOfBoundsException();
+ }
+
+ /**
+ * Writes the contents of this CharArrayWriter to another Writer. The output
+ * is all the characters that have been written to the receiver since the
+ * last reset or since the creation.
+ *
+ * @param out
+ * the non-null Writer on which to write the contents.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write the contents out.
+ */
+ public void writeTo(Writer out) throws IOException {
+ synchronized (lock) {
+ out.write(buf, 0, count);
+ }
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharConversionException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharConversionException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharConversionException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/CharConversionException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,44 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * This is the top level class for character conversion exceptions.
+ *
+ */
+public class CharConversionException extends IOException {
+
+ /**
+ * Constructs a new instance of this class with its walkback filled in.
+ *
+ */
+ public CharConversionException() {
+ super();
+ }
+
+ /**
+ * Constructs a new instance of this class with its walkback and message
+ * filled in.
+ *
+ * @param detailMessage
+ * The detail message for the exception.
+ */
+ public CharConversionException(String detailMessage) {
+ super(detailMessage);
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInput.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInput.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInput.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInput.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,230 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * DataInput is an interface which declares methods for reading in typed data
+ * from a Stream. Typically, this stream has been written by a class which
+ * implements DataOutput. Types that can be read include byte, 16-bit short,
+ * 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, and UTF
+ * Strings.
+ *
+ * @see DataInputStream
+ * @see RandomAccessFile
+ */
+public interface DataInput {
+ /**
+ * Reads a boolean from this stream.
+ *
+ * @return the next boolean value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeBoolean(boolean)
+ */
+ public abstract boolean readBoolean() throws IOException;
+
+ /**
+ * Reads an 8-bit byte value from this stream.
+ *
+ * @return the next byte value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeByte(int)
+ */
+ public abstract byte readByte() throws IOException;
+
+ /**
+ * Reads a 16-bit character value from this stream.
+ *
+ * @return the next <code>char</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeChar(int)
+ */
+ public abstract char readChar() throws IOException;
+
+ /**
+ * Reads a 64-bit <code>double</code> value from this stream.
+ *
+ * @return the next <code>double</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeDouble(double)
+ */
+ public abstract double readDouble() throws IOException;
+
+ /**
+ * Reads a 32-bit <code>float</code> value from this stream.
+ *
+ * @return the next <code>float</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeFloat(float)
+ */
+ public abstract float readFloat() throws IOException;
+
+ /**
+ * Reads bytes from this stream into the byte array <code>buffer</code>.
+ * This method will block until <code>buffer.length</code> number of bytes
+ * have been read.
+ *
+ * @param buffer
+ * the buffer to read bytes into
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#write(byte[])
+ * @see DataOutput#write(byte[], int, int)
+ */
+ public abstract void readFully(byte[] buffer) throws IOException;
+
+ /**
+ * Read bytes from this stream and stores them in byte array
+ * <code>buffer</code> starting at offset <code>offset</code>. This
+ * method blocks until <code>count</code> number of bytes have been read.
+ *
+ * @param buffer
+ * the byte array in which to store the read bytes.
+ * @param offset
+ * the offset in <code>buffer</code> to store the read bytes.
+ * @param count
+ * the maximum number of bytes to store in <code>buffer</code>.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#write(byte[])
+ * @see DataOutput#write(byte[], int, int)
+ */
+ public abstract void readFully(byte[] buffer, int offset, int count)
+ throws IOException;
+
+ /**
+ * Reads a 32-bit integer value from this stream.
+ *
+ * @return the next <code>int</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeInt(int)
+ */
+ public abstract int readInt() throws IOException;
+
+ /**
+ * Answers a <code>String</code> representing the next line of text
+ * available in this BufferedReader. A line is represented by 0 or more
+ * characters followed by <code>'\n'</code>, <code>'\r'</code>,
+ * <code>"\n\r"</code> or end of stream. The <code>String</code> does
+ * not include the newline sequence.
+ *
+ * @return the contents of the line or null if no characters were read
+ * before end of stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ */
+ public abstract String readLine() throws IOException;
+
+ /**
+ * Reads a 64-bit <code>long</code> value from this stream.
+ *
+ * @return the next <code>long</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeLong(long)
+ */
+ public abstract long readLong() throws IOException;
+
+ /**
+ * Reads a 16-bit <code>short</code> value from this stream.
+ *
+ * @return the next <code>short</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeShort(int)
+ */
+ public abstract short readShort() throws IOException;
+
+ /**
+ * Reads an unsigned 8-bit <code>byte</code> value from this stream and
+ * returns it as an int.
+ *
+ * @return the next unsigned byte value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeByte(int)
+ */
+ public abstract int readUnsignedByte() throws IOException;
+
+ /**
+ * Reads a 16-bit unsigned <code>short</code> value from this stream and
+ * returns it as an int.
+ *
+ * @return the next unsigned <code>short</code> value from the source
+ * stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeShort(int)
+ */
+ public abstract int readUnsignedShort() throws IOException;
+
+ /**
+ * Reads a UTF format String from this Stream.
+ *
+ * @return the next UTF String from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ *
+ * @see DataOutput#writeUTF(java.lang.String)
+ */
+ public abstract String readUTF() throws IOException;
+
+ /**
+ * Skips <code>count</code> number of bytes in this stream. Subsequent
+ * <code>read()</code>'s will not return these bytes unless
+ * <code>reset()</code> is used.
+ *
+ * @param count
+ * the number of bytes to skip.
+ * @return the number of bytes actually skipped.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this stream.
+ */
+ public abstract int skipBytes(int count) throws IOException;
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInputStream.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInputStream.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataInputStream.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,452 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * DataInputStream is a filter class which can read typed data from a Stream.
+ * Typically, this stream has been written by a DataOutputStream. Types that can
+ * be read include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
+ * 64-bit double, byte strings, and UTF Strings.
+ *
+ * @see DataOutputStream
+ */
+public class DataInputStream extends FilterInputStream implements DataInput {
+ /**
+ * Constructs a new DataInputStream on the InputStream <code>in</code>.
+ * All reads can now be filtered through this stream. Note that data read by
+ * this Stream is not in a human readable format and was most likely created
+ * by a DataOutputStream.
+ *
+ * @param in
+ * the target InputStream to filter reads on.
+ *
+ * @see DataOutputStream
+ * @see RandomAccessFile
+ */
+ public DataInputStream(InputStream in) {
+ super(in);
+ }
+
+ /**
+ * Reads bytes from the source stream into the byte array
+ * <code>buffer</code>. The number of bytes actually read is returned.
+ *
+ * @param buffer
+ * the buffer to read bytes into
+ * @return the number of bytes actually read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#write(byte[])
+ * @see DataOutput#write(byte[], int, int)
+ */
+ public final int read(byte[] buffer) throws IOException {
+ return in.read(buffer, 0, buffer.length);
+ }
+
+ /**
+ * Read at most <code>length</code> bytes from this DataInputStream and
+ * stores them in byte array <code>buffer</code> starting at
+ * <code>offset</code>. Answer the number of bytes actually read or -1 if
+ * no bytes were read and end of stream was encountered.
+ *
+ * @param buffer
+ * the byte array in which to store the read bytes.
+ * @param offset
+ * the offset in <code>buffer</code> to store the read bytes.
+ * @param length
+ * the maximum number of bytes to store in <code>buffer</code>.
+ * @return the number of bytes actually read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#write(byte[])
+ * @see DataOutput#write(byte[], int, int)
+ */
+ public final int read(byte[] buffer, int offset, int length)
+ throws IOException {
+ return in.read(buffer, offset, length);
+ }
+
+ /**
+ * Reads a boolean from this stream.
+ *
+ * @return the next boolean value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeBoolean(boolean)
+ */
+ public final boolean readBoolean() throws IOException {
+ int temp = in.read();
+ if (temp >= 0)
+ return temp != 0;
+ throw new EOFException();
+ }
+
+ /**
+ * Reads an 8-bit byte value from this stream.
+ *
+ * @return the next byte value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeByte(int)
+ */
+ public final byte readByte() throws IOException {
+ int temp = in.read();
+ if (temp >= 0)
+ return (byte) temp;
+ throw new EOFException();
+ }
+
+ /**
+ * Reads a 16-bit character value from this stream.
+ *
+ * @return the next <code>char</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeChar(int)
+ */
+ public final char readChar() throws IOException {
+ int b1 = in.read();
+ int b2 = in.read();
+ if ((b1 | b2) >= 0)
+ return (char) ((b1 << 8) + b2);
+ throw new EOFException();
+ }
+
+ /**
+ * Reads a 64-bit <code>double</code> value from this stream.
+ *
+ * @return the next <code>double</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeDouble(double)
+ */
+ public final double readDouble() throws IOException {
+ return Double.longBitsToDouble(readLong());
+ }
+
+ /**
+ * Reads a 32-bit <code>float</code> value from this stream.
+ *
+ * @return the next <code>float</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeFloat(float)
+ */
+ public final float readFloat() throws IOException {
+ return Float.intBitsToFloat(readInt());
+ }
+
+ /**
+ * Reads bytes from this stream into the byte array <code>buffer</code>.
+ * This method will block until <code>buffer.length</code> number of bytes
+ * have been read.
+ *
+ * @param buffer
+ * to read bytes into
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#write(byte[])
+ * @see DataOutput#write(byte[], int, int)
+ */
+ public final void readFully(byte[] buffer) throws IOException {
+ readFully(buffer, 0, buffer.length);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readFully(byte[], int, int)
+ */
+ public final void readFully(byte[] buffer, int offset, int length)
+ throws IOException {
+ if (buffer != null) {
+ // avoid int overflow
+ if (0 <= offset && offset <= buffer.length && 0 <= length
+ && length <= buffer.length - offset) {
+ while (length > 0) {
+ int result = in.read(buffer, offset, length);
+ if (result >= 0) {
+ offset += result;
+ length -= result;
+ } else
+ throw new EOFException();
+ }
+ } else
+ throw new IndexOutOfBoundsException();
+ } else
+ throw new NullPointerException(com.ibm.oti.util.Msg
+ .getString("K0047")); //$NON-NLS-1$
+ }
+
+ /**
+ * Reads a 32-bit integer value from this stream.
+ *
+ * @return the next <code>int</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeInt(int)
+ */
+ public final int readInt() throws IOException {
+ int b1 = in.read();
+ int b2 = in.read();
+ int b3 = in.read();
+ int b4 = in.read();
+ if ((b1 | b2 | b3 | b4) >= 0)
+ return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
+ throw new EOFException();
+ }
+
+ /**
+ * Answers a <code>String</code> representing the next line of text
+ * available in this BufferedReader. A line is represented by 0 or more
+ * characters followed by <code>'\n'</code>, <code>'\r'</code>,
+ * <code>"\n\r"</code> or end of stream. The <code>String</code> does
+ * not include the newline sequence.
+ *
+ * @return the contents of the line or null if no characters were read
+ * before end of stream.
+ *
+ * @throws IOException
+ * If the DataInputStream is already closed or some other IO
+ * error occurs.
+ *
+ * @deprecated Use BufferedReader
+ */
+ public final String readLine() throws IOException {
+ StringBuffer line = new StringBuffer(80); // Typical line length
+ boolean foundTerminator = false;
+ while (true) {
+ int nextByte = in.read();
+ switch (nextByte) {
+ case -1:
+ if (line.length() == 0 && !foundTerminator)
+ return null;
+ return line.toString();
+ case (byte) '\r':
+ if (foundTerminator) {
+ ((PushbackInputStream) in).unread(nextByte);
+ return line.toString();
+ }
+ foundTerminator = true;
+ /* Have to be able to peek ahead one byte */
+ if (!(in.getClass() == PushbackInputStream.class))
+ in = new PushbackInputStream(in);
+ break;
+ case (byte) '\n':
+ return line.toString();
+ default:
+ if (foundTerminator) {
+ ((PushbackInputStream) in).unread(nextByte);
+ return line.toString();
+ }
+ line.append((char) nextByte);
+ }
+ }
+ }
+
+ /**
+ * Reads a 64-bit <code>long</code> value from this stream.
+ *
+ * @return the next <code>long</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeLong(long)
+ */
+ public final long readLong() throws IOException {
+ int i1 = readInt();
+ int b1 = in.read();
+ int b2 = in.read();
+ int b3 = in.read();
+ int b4 = in.read();
+ if ((b1 | b2 | b3 | b4) >= 0)
+ return (((long) i1) << 32) + ((long) b1 << 24) + (b2 << 16)
+ + (b3 << 8) + b4;
+ throw new EOFException();
+ }
+
+ /**
+ * Reads a 16-bit <code>short</code> value from this stream.
+ *
+ * @return the next <code>short</code> value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeShort(int)
+ */
+ public final short readShort() throws IOException {
+ int b1 = in.read();
+ int b2 = in.read();
+ if ((b1 | b2) >= 0)
+ return (short) ((b1 << 8) + b2);
+ throw new EOFException();
+ }
+
+ /**
+ * Reads an unsigned 8-bit <code>byte</code> value from this stream and
+ * returns it as an int.
+ *
+ * @return the next unsigned byte value from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeByte(int)
+ */
+ public final int readUnsignedByte() throws IOException {
+ int temp = in.read();
+ if (temp >= 0)
+ return temp;
+ throw new EOFException();
+ }
+
+ /**
+ * Reads a 16-bit unsigned <code>short</code> value from this stream and
+ * returns it as an int.
+ *
+ * @return the next unsigned <code>short</code> value from the source
+ * stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeShort(int)
+ */
+ public final int readUnsignedShort() throws IOException {
+ int b1 = in.read();
+ int b2 = in.read();
+ if ((b1 | b2) >= 0)
+ return ((b1 << 8) + b2);
+ throw new EOFException();
+ }
+
+ /**
+ * Reads a UTF format String from this Stream.
+ *
+ * @return the next UTF String from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeUTF(java.lang.String)
+ */
+ public final String readUTF() throws IOException {
+ int utfSize = readUnsignedShort();
+ return decodeUTF(utfSize);
+ }
+
+ static final int MAX_BUF_SIZE = 8192;
+
+ static boolean useShared = true;
+
+ static byte[] byteBuf = new byte[0];
+
+ static char[] charBuf = new char[0];
+
+ String decodeUTF(int utfSize) throws IOException {
+ byte[] buf;
+ char[] out = null;
+ boolean makeBuf = true;
+ if (utfSize <= MAX_BUF_SIZE && useShared) {
+ synchronized (byteBuf) {
+ if (useShared) {
+ useShared = false;
+ makeBuf = false;
+ }
+ }
+ }
+ if (makeBuf) {
+ buf = new byte[utfSize];
+ out = new char[utfSize];
+ } else {
+ if (byteBuf.length < utfSize)
+ byteBuf = new byte[utfSize];
+ if (charBuf.length < utfSize) {
+ charBuf = new char[utfSize];
+ }
+ buf = byteBuf;
+ out = charBuf;
+ }
+
+ readFully(buf, 0, utfSize);
+ String result;
+ result = com.ibm.oti.util.Util.convertUTF8WithBuf(buf, out, 0, utfSize);
+ if (!makeBuf)
+ useShared = true;
+ return result;
+ }
+
+ /**
+ * Reads a UTF format String from the DataInput Stream <code>in</code>.
+ *
+ * @param in
+ * the input stream to read from
+ * @return the next UTF String from the source stream.
+ *
+ * @throws IOException
+ * If a problem occurs reading from this DataInputStream.
+ *
+ * @see DataOutput#writeUTF(java.lang.String)
+ */
+ public static final String readUTF(DataInput in) throws IOException {
+ return in.readUTF();
+ }
+
+ /**
+ * Skips <code>count</code> number of bytes in this stream. Subsequent
+ * <code>read()</code>'s will not return these bytes unless
+ * <code>reset()</code> is used.
+ *
+ * @param count
+ * the number of bytes to skip.
+ * @return the number of bytes actually skipped.
+ *
+ * @throws IOException
+ * If the stream is already closed or another IOException
+ * occurs.
+ */
+ public final int skipBytes(int count) throws IOException {
+ int skipped = 0;
+ long skip;
+ while (skipped < count && (skip = in.skip(count - skipped)) != 0)
+ skipped += skip;
+ if (skipped >= 0)
+ return skipped;
+ throw new EOFException();
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutput.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutput.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutput.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutput.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,234 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * DataOutput is an interface which declares methods for writing typed data to a
+ * Stream. Typically, this stream can be read in by a class which implements
+ * DataInput. Types that can be written include byte, 16-bit short, 32-bit int,
+ * 32-bit float, 64-bit long, 64-bit double, byte strings, and UTF Strings.
+ *
+ * @see DataOutputStream
+ * @see RandomAccessFile
+ */
+public interface DataOutput {
+
+ /**
+ * Writes the entire contents of the byte array <code>buffer</code> to the
+ * OutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[], int, int)
+ */
+ public abstract void write(byte buffer[]) throws IOException;
+
+ /**
+ * Writes <code>count</code> <code>bytes</code> from the byte array
+ * <code>buffer</code> starting at offset <code>index</code> to the
+ * OutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ * @param offset
+ * offset in buffer to get bytes
+ * @param count
+ * number of bytes in buffer to write
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[], int, int)
+ */
+ public abstract void write(byte buffer[], int offset, int count)
+ throws IOException;
+
+ /**
+ * Writes the specified <code>byte</code> to the OutputStream.
+ *
+ * @param oneByte
+ * the byte to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readByte()
+ */
+ public abstract void write(int oneByte) throws IOException;
+
+ /**
+ * Writes a boolean to this output stream.
+ *
+ * @param val
+ * the boolean value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readBoolean()
+ */
+ public abstract void writeBoolean(boolean val) throws IOException;
+
+ /**
+ * Writes a 8-bit byte to this output stream.
+ *
+ * @param val
+ * the byte value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readByte()
+ * @see DataInput#readUnsignedByte()
+ */
+ public abstract void writeByte(int val) throws IOException;
+
+ /**
+ * Writes the low order 8-bit bytes from a String to this output stream.
+ *
+ * @param str
+ * the String containing the bytes to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[],int,int)
+ */
+ public abstract void writeBytes(String str) throws IOException;
+
+ /**
+ * Writes the specified 16-bit character to the OutputStream. Only the lower
+ * 2 bytes are written with the higher of the 2 bytes written first. This
+ * represents the Unicode value of val.
+ *
+ * @param oneByte
+ * the character to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readChar()
+ */
+ public abstract void writeChar(int oneByte) throws IOException;
+
+ /**
+ * Writes the specified 16-bit characters contained in str to the
+ * OutputStream. Only the lower 2 bytes of each character are written with
+ * the higher of the 2 bytes written first. This represents the Unicode
+ * value of each character in str.
+ *
+ * @param str
+ * the String whose characters are to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readChar()
+ */
+ public abstract void writeChars(String str) throws IOException;
+
+ /**
+ * Writes a 64-bit double to this output stream. The resulting output is the
+ * 8 bytes resulting from calling Double.doubleToLongBits().
+ *
+ * @param val
+ * the double to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readDouble()
+ */
+ public abstract void writeDouble(double val) throws IOException;
+
+ /**
+ * Writes a 32-bit float to this output stream. The resulting output is the
+ * 4 bytes resulting from calling Float.floatToIntBits().
+ *
+ * @param val
+ * the float to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readFloat()
+ */
+ public abstract void writeFloat(float val) throws IOException;
+
+ /**
+ * Writes a 32-bit int to this output stream. The resulting output is the 4
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the int to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readInt()
+ */
+ public abstract void writeInt(int val) throws IOException;
+
+ /**
+ * Writes a 64-bit long to this output stream. The resulting output is the 8
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the long to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readLong()
+ */
+ public abstract void writeLong(long val) throws IOException;
+
+ /**
+ * Writes the specified 16-bit short to the OutputStream. Only the lower 2
+ * bytes are written with the higher of the 2 bytes written first.
+ *
+ * @param val
+ * the short to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readShort()
+ * @see DataInput#readUnsignedShort()
+ */
+ public abstract void writeShort(int val) throws IOException;
+
+ /**
+ * Writes the specified String out in UTF format.
+ *
+ * @param str
+ * the String to be written in UTF format.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this stream.
+ *
+ * @see DataInput#readUTF()
+ */
+ public abstract void writeUTF(String str) throws IOException;
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutputStream.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutputStream.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutputStream.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/DataOutputStream.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,438 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * DataOutputStream is a filter class which can write typed data to a Stream.
+ * Typically, this stream can be read in by a DataInputStream. Types that can be
+ * written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long,
+ * 64-bit double, byte strings, and UTF Strings.
+ *
+ * @see DataInputStream
+ */
+public class DataOutputStream extends FilterOutputStream implements DataOutput {
+ /** The number of bytes written out so far */
+ protected int written;
+
+ /**
+ * Constructs a new DataOutputStream on the OutputStream <code>out</code>.
+ * All writes can now be filtered through this stream. Note that data
+ * written by this Stream is not in a human readable format but can be
+ * reconstructed by using a DataInputStream on the resulting output.
+ *
+ * @param out
+ * the target OutputStream to filter writes on.
+ */
+ public DataOutputStream(OutputStream out) {
+ super(out);
+ }
+
+ /**
+ * Flush this DataOutputStream to ensure all pending data is sent out to the
+ * target OutputStream. This implementation flushes the target OutputStream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to flush this DataOutputStream.
+ */
+ public void flush() throws IOException {
+ super.flush();
+ }
+
+ /**
+ * Answers the total number of bytes written to this stream thus far.
+ *
+ * @return the number of bytes written to this DataOutputStream.
+ */
+ public final int size() {
+ if (written < 0)
+ written = Integer.MAX_VALUE;
+ return written;
+ }
+
+ /**
+ * Writes <code>count</code> <code>bytes</code> from the byte array
+ * <code>buffer</code> starting at offset <code>index</code> to the
+ * OutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ * @param offset
+ * offset in buffer to get bytes
+ * @param count
+ * number of bytes in buffer to write
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[], int, int)
+ */
+ public void write(byte buffer[], int offset, int count) throws IOException {
+ if (buffer != null) {
+ out.write(buffer, offset, count);
+ written += count;
+ } else
+ throw new NullPointerException(com.ibm.oti.util.Msg
+ .getString("K0047")); //$NON-NLS-1$
+ }
+
+ /**
+ * Writes the specified <code>byte</code> to the OutputStream.
+ *
+ * @param oneByte
+ * the byte to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readByte()
+ */
+ public void write(int oneByte) throws IOException {
+ out.write(oneByte);
+ written++;
+ }
+
+ /**
+ * Writes a boolean to this output stream.
+ *
+ * @param val
+ * the boolean value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readBoolean()
+ */
+ public final void writeBoolean(boolean val) throws IOException {
+ out.write(val ? 1 : 0);
+ written++;
+ }
+
+ /**
+ * Writes a 8-bit byte to this output stream.
+ *
+ * @param val
+ * the byte value to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readByte()
+ * @see DataInput#readUnsignedByte()
+ */
+ public final void writeByte(int val) throws IOException {
+ out.write(val);
+ written++;
+ }
+
+ /**
+ * Writes the low order 8-bit bytes from a String to this output stream.
+ *
+ * @param str
+ * the String containing the bytes to write to the OutputStream
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFully(byte[])
+ * @see DataInput#readFully(byte[],int,int)
+ */
+ public final void writeBytes(String str) throws IOException {
+ byte bytes[] = new byte[str.length()];
+ for (int index = 0; index < str.length(); index++)
+ bytes[index] = (byte) str.charAt(index);
+ out.write(bytes);
+ written += bytes.length;
+ }
+
+ /**
+ * Writes the specified 16-bit character to the OutputStream. Only the lower
+ * 2 bytes are written with the higher of the 2 bytes written first. This
+ * represents the Unicode value of val.
+ *
+ * @param val
+ * the character to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readChar()
+ */
+ public final void writeChar(int val) throws IOException {
+ out.write(val >> 8);
+ out.write(val);
+ written += 2;
+ }
+
+ /**
+ * Writes the specified 16-bit characters contained in str to the
+ * OutputStream. Only the lower 2 bytes of each character are written with
+ * the higher of the 2 bytes written first. This represents the Unicode
+ * value of each character in str.
+ *
+ * @param str
+ * the String whose characters are to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readChar()
+ */
+ public final void writeChars(String str) throws IOException {
+ byte newBytes[] = new byte[str.length() * 2];
+ for (int index = 0; index < str.length(); index++) {
+ int newIndex = index == 0 ? index : index * 2;
+ newBytes[newIndex] = (byte) (str.charAt(index) >> 8);
+ newBytes[newIndex + 1] = (byte) str.charAt(index);
+ }
+ out.write(newBytes);
+ written += newBytes.length;
+ }
+
+ /**
+ * Writes a 64-bit double to this output stream. The resulting output is the
+ * 8 bytes resulting from calling Double.doubleToLongBits().
+ *
+ * @param val
+ * the double to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readDouble()
+ */
+ public final void writeDouble(double val) throws IOException {
+ writeLong(Double.doubleToLongBits(val));
+ }
+
+ /**
+ * Writes a 32-bit float to this output stream. The resulting output is the
+ * 4 bytes resulting from calling Float.floatToIntBits().
+ *
+ * @param val
+ * the float to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readFloat()
+ */
+ public final void writeFloat(float val) throws IOException {
+ writeInt(Float.floatToIntBits(val));
+ }
+
+ /**
+ * Writes a 32-bit int to this output stream. The resulting output is the 4
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the int to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readInt()
+ */
+ public final void writeInt(int val) throws IOException {
+ out.write(val >> 24);
+ out.write(val >> 16);
+ out.write(val >> 8);
+ out.write(val);
+ written += 4;
+ }
+
+ /**
+ * Writes a 64-bit long to this output stream. The resulting output is the 8
+ * bytes, highest order first, of val.
+ *
+ * @param val
+ * the long to be written.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readLong()
+ */
+ public final void writeLong(long val) throws IOException {
+ writeInt((int) (val >> 32));
+ writeInt((int) val);
+ }
+
+ /**
+ * Writes the specified 16-bit short to the OutputStream. Only the lower 2
+ * bytes are written with the higher of the 2 bytes written first.
+ *
+ * @param val
+ * the short to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readShort()
+ * @see DataInput#readUnsignedShort()
+ */
+ public final void writeShort(int val) throws IOException {
+ writeChar(val);
+ }
+
+ /**
+ * Writes the specified String out in UTF format.
+ *
+ * @param str
+ * the String to be written in UTF format.
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * DataOutputStream.
+ *
+ * @see DataInput#readUTF()
+ */
+ public final void writeUTF(String str) throws IOException {
+ int length = str.length();
+ if (length <= DataInputStream.MAX_BUF_SIZE / 3) {
+ int size = length * 3;
+ byte[] utfBytes;
+ boolean makeBuf = true;
+ synchronized (DataInputStream.byteBuf) {
+ if (DataInputStream.useShared) {
+ DataInputStream.useShared = false;
+ makeBuf = false;
+ }
+ }
+ if (makeBuf) {
+ utfBytes = new byte[size];
+ } else {
+ if (DataInputStream.byteBuf.length < size)
+ DataInputStream.byteBuf = new byte[size];
+ utfBytes = DataInputStream.byteBuf;
+ }
+ int utfIndex = 0;
+ for (int i = 0; i < length; i++) {
+ int charValue = str.charAt(i);
+ if (charValue > 0 && charValue <= 127) {
+ utfBytes[utfIndex++] = (byte) charValue;
+ } else if (charValue <= 2047) {
+ utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
+ } else {
+ utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
+ }
+ }
+ writeShort(utfIndex);
+ write(utfBytes, 0, utfIndex);
+ if (!makeBuf)
+ DataInputStream.useShared = true;
+ } else {
+ long utfCount;
+ if (length <= 65535 && (utfCount = countUTFBytes(str)) <= 65535) {
+ writeShort((int) utfCount);
+ writeUTFBytes(str, utfCount);
+ } else
+ throw new UTFDataFormatException(com.ibm.oti.util.Msg
+ .getString("K0068")); //$NON-NLS-1$
+ }
+ }
+
+ long countUTFBytes(String str) {
+ int utfCount = 0, length = str.length();
+ for (int i = 0; i < length; i++) {
+ int charValue = str.charAt(i);
+ if (charValue > 0 && charValue <= 127)
+ utfCount++;
+ else if (charValue <= 2047)
+ utfCount += 2;
+ else
+ utfCount += 3;
+ }
+ return utfCount;
+ }
+
+ void writeUTFBytes(String str, long count) throws IOException {
+ boolean single = true;
+ int size = (int) count;
+ if (count > DataInputStream.MAX_BUF_SIZE) {
+ single = false;
+ size = DataInputStream.MAX_BUF_SIZE;
+ }
+ byte[] utfBytes;
+ boolean makeBuf = true;
+ if (DataInputStream.useShared) {
+ synchronized (DataInputStream.byteBuf) {
+ if (DataInputStream.useShared) {
+ DataInputStream.useShared = false;
+ makeBuf = false;
+ }
+ }
+ }
+ if (makeBuf) {
+ utfBytes = new byte[size];
+ } else {
+ if (DataInputStream.byteBuf.length < size)
+ DataInputStream.byteBuf = new byte[size];
+ utfBytes = DataInputStream.byteBuf;
+ }
+
+ int utfIndex = 0, i = 0, length = str.length();
+ int end = length;
+ while (i < length) {
+ if (!single) {
+ end = i + ((utfBytes.length - utfIndex) / 3);
+ if (end > length)
+ end = length;
+ }
+ for (int j = i; j < end; j++) {
+ int charValue = str.charAt(j);
+ if (charValue > 0 && charValue <= 127) {
+ utfBytes[utfIndex++] = (byte) charValue;
+ } else if (charValue <= 2047) {
+ utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
+ } else {
+ utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & charValue));
+ }
+ }
+ if (single || utfIndex > utfBytes.length - 300) {
+ write(utfBytes, 0, utfIndex);
+ if (single)
+ return;
+ utfIndex = 0;
+ }
+ i = end;
+ }
+ if (utfIndex > 0)
+ write(utfBytes, 0, utfIndex);
+ if (!makeBuf)
+ DataInputStream.useShared = true;
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/EOFException.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/EOFException.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/EOFException.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/io/EOFException.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,46 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package java.io;
+
+
+/**
+ * This End Of File (EOF) exception is thrown when a program encounters the end
+ * of a file or stream during an operation.
+ *
+ */
+public class EOFException extends IOException {
+
+ /**
+ * Constructs a new instance of this class with its walkback filled in.
+ *
+ */
+ public EOFException() {
+ super();
+ }
+
+ /**
+ * Constructs a new instance of this class with its walkback and message
+ * filled in.
+ *
+ * @param detailMessage
+ * The detail message for the exception.
+ */
+ public EOFException(String detailMessage) {
+ super(detailMessage);
+ }
+
+}