Author: mturk
Date: Tue Jul 12 21:44:08 2011
New Revision: 1145784
URL: http://svn.apache.org/viewvc?rev=1145784&view=rev
Log:
Add io and net stream base skeletons
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
(with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
(with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
(with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
(with props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java?rev=1145784&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
Tue Jul 12 21:44:08 2011
@@ -0,0 +1,255 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime.io;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.Pointer;
+
+/**
+ * Reader interface.
+ */
+public interface Reader
+{
+
+ /**
+ * Returns the number of bytes that are available before this stream will
+ * block.
+ *
+ * @return the number of bytes available before blocking.
+ * @throws IOException
+ * if an error occurs in this stream.
+ */
+ public int available()
+ throws IOException;
+
+ /**
+ * Reads a single byte from the current position in this stream and returns
+ * it as an integer in the range from 0 to 255. Returns {@code -1} if the
+ * end of the stream has been reached. If the stream is in blocking mode,
+ * it blocks until one byte has been read, the end of the stream is
+ * detected or an exception is thrown.
+ *
+ * @return The byte read or {@code -1} if the end of the stream has
+ * been reached.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read()
+ throws IOException;
+
+ /**
+ * Reads bytes from the current position in this stream and stores them
+ * in the byte array {@code buffer}.
+ * The maximum number of bytes read corresponds
+ * to the size of {@code buffer}. Blocks until at least one byte has been
+ * read if the stream is in blocking mode.
+ *
+ * @param buffer
+ * The byte array in which to store the bytes read.
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read(byte[] buffer)
+ throws IOException;
+
+ /**
+ * Reads at most {@code count} bytes from the current position in this
+ * stream and stores them in the byte array {@code buffer} starting at
+ * {@code offset}. Blocks until {@code count} bytes have been read,
+ * the end of the stream is reached or an exception is thrown.
+ *
+ * @param buffer
+ * The array in which to store the bytes read from this stream.
+ * @param offset
+ * The initial position in {@code buffer} to store the bytes read
+ * from this stream.
+ * @param count
+ * The maximum number of bytes to store in {@code buffer}.
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code buffer}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read(byte[] buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+ /**
+ * Reads bytes from the current position in this stream and stores them
+ * in the {@link Pointer} {@code pointer}.
+ * The maximum number of bytes read corresponds
+ * to the size of {@code pointer}. Blocks until at least one byte has been
+ * read if the stream is in blocking mode.
+ *
+ * @param pointer
+ * The {@code Pointer} in which to store the bytes read.
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws NullPointerException
+ * If {@code pointer} is {@code null}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long read(Pointer pointer)
+ throws NullPointerException, IOException;
+
+ /**
+ * Reads at most {@code count} bytes from the current position in
+ * this stream and stores them in the {@link Pointer} {@code pointer}
+ * starting at {@code offset}. Blocks until {@code count} bytes have
+ * been read, the end of the stream is reached or an exception is thrown.
+ *
+ * @param pointer
+ * The {@code Pointer} in which to store the bytes read from
+ * this file.
+ * @param offset
+ * The initial position in {@code pointer} to store the bytes read
+ * from this stream.
+ * @param count
+ * The maximum number of bytes to store in {@code pointer}.
+ *
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws NullPointerException
+ * If {@code pointer} is {@code null}.
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code buffer}.
+ * @throws ClosedDescriptorException
+ * If this file is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long read(Pointer pointer, long offset, long count)
+ throws NullPointerException, IndexOutOfBoundsException, IOException;
+
+ /**
+ * Reads bytes from the current position in this stream and stores them
+ * in the {@code ByteBuffer} {@code buffer}. The maximum number of bytes
+ * read corresponds to the size of {@code buffer}.
+ * Blocks until at least one byte has been read if the stream is in
+ * blocking mode.
+ * <p>
+ * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
+ * or obtained from {@link NioByteBuffer}.
+ * </p>
+ *
+ * @param buffer
+ * The {@code ByteBuffer} in which to store the bytes read.
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read(ByteBuffer buffer)
+ throws IOException;
+
+ /**
+ * Reads at most {@code count} bytes from the current position in this
+ * stream and stores them in the {@code ByteBuffer} {@code buffer}
+ * starting at {@code offset}.
+ * Blocks until {@code count} bytes have been read, the end of the stream
+ * is reached or an exception is thrown.
+ * <p>
+ * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
+ * or obtained from {@link NioByteBuffer}.
+ * </p>
+ *
+ * @param buffer
+ * The {@code ByteBuffer} in which to store the bytes read from
+ * this stream.
+ * @param offset
+ * The initial position in {@code buffer} to store the bytes read
+ * from this stream.
+ * @param count
+ * The maximum number of bytes to store in {@code buffer}.
+ *
+ * @return The number of bytes actually read or {@code -1} if the end of
+ * the stream has been reached.
+ *
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code buffer}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read(ByteBuffer buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java?rev=1145784&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
Tue Jul 12 21:44:08 2011
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime.io;
+
+import java.io.IOException;
+import java.io.SyncFailedException;
+
+/**
+ * Bidirectional Stream.
+ * <p>
+ * This class is combination of the Java InputStream and OutputStream
+ * minus mark interface.
+ * </p>
+ */
+public abstract class Stream
+ implements Streamable, Reader, Writer
+{
+
+ /**
+ * Creates a new object instance.
+ */
+ public Stream()
+ {
+
+ }
+
+ /**
+ * Returns the number of bytes that are available before this stream will
+ * block.
+ *
+ * @return the number of bytes available before blocking.
+ * @throws IOException
+ * if an error occurs in this stream.
+ */
+ public int available()
+ throws IOException
+ {
+ return 0;
+ }
+
+ /**
+ * Closes the object and release any system resources it holds. If the
+ * object has already been closed, then invoking this method has no effect.
+ *
+ * @throws IOException
+ * if any error occurs when closing the object.
+ */
+ public abstract void close()
+ throws IOException;
+
+ /**
+ * Flush the underlying stream metadata.
+ * <p>
+ * {@code flush} transfers all modified metadata of the stream object
+ * referred to by {@code this} stream to the disk device
+ * (or other storage device) where that object resides.
+ * The call blocks until the device reports that the transfer has
+ * completed. It also flushes metadata information associated with
+ * {@code this} Descriptor.
+ * </p>
+ *
+ * @throws SyncFailedException when the object cannot be flushed.
+ * @throws IOException if an I/O error occurs.
+ */
+ public abstract void flush()
+ throws SyncFailedException, IOException;
+
+ /**
+ * Sync the underlying stream by writing any buffered data.
+ * <p>
+ * {@code sync} transfers all modified in-core data of the stream object
+ * referred to by {@code this} stream to the disk device
+ * (or other storage device) where that object resides.
+ * The call blocks until the device reports that the transfer has
+ * completed. It also flushes metadata information associated with
+ * {@code this} Descriptor.
+ * </p>
+ *
+ * @throws SyncFailedException when the object cannot be flushed.
+ * @throws IOException if an I/O error occurs.
+ */
+ public abstract void sync()
+ throws SyncFailedException, IOException;
+
+ /**
+ * Test if {@code this} stream is valid.
+ *
+ * @return {@code true} if the stream represents a valid,
+ * open file, socket, or other I/O object; {@code false} otherwse.
+ *
+ */
+ public abstract boolean valid();
+
+ /**
+ * Test wather or not every I/O operation on {@code this} stream will
+ * block until it completes.
+ *
+ * @return {@code true} if, and only if, this stream
+ * is in blocking mode.
+ *
+ * @throws IOException if an I/O error occurs.
+ */
+ public boolean isBlocking()
+ throws IOException
+ {
+ return false;
+ }
+
+ /**
+ * Skips over {@code count} bytes in this stream. Less than {@code count}
+ * bytes are skipped if the end of the stream is reached or an exception is
+ * thrown during the operation. Nothing is done if {@code count} is
+ * negative.
+ *
+ * @param count
+ * The number of bytes to skip.
+ *
+ * @return The number of bytes actually skipped.
+ *
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public abstract long skip(long count)
+ throws IOException;
+
+ /**
+ * Called by the garbage collector when the object is destroyed.
+ * The class will free internal resources allocated by the
+ * Operating system only if there are no additional references
+ * to this object.
+ * @see Object#finalize()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ @Override
+ protected final void finalize()
+ throws Throwable
+ {
+ try {
+ close();
+ } catch (Exception x) {
+ // Ignore exceptions on finalize
+ }
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java?rev=1145784&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
Tue Jul 12 21:44:08 2011
@@ -0,0 +1,256 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime.io;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.Pointer;
+
+/**
+ * Writer
+ */
+public interface Writer extends Syncable
+{
+
+ /**
+ * Writes a byte to this stream, starting at the current file pointer.
+ * Only the least significant byte of the integer {@code b} is written.
+ *
+ * @param b
+ * the byte to write to this stream.
+ *
+ * @return The number of bytes actually written.
+ *
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int write(int b)
+ throws IOException;
+
+ /**
+ * Writes {@code count} bytes from the byte array {@code buffer} to this
+ * stream, starting at the current file pointer and using {@code offset}
+ * as the first position within {@code buffer} to get bytes.
+ *
+ * @param buffer
+ * The buffer to write to this stream.
+ * @param offset
+ * The index of the first byte in {@code buffer} to write.
+ * @param count
+ * The number of bytes from {@code buffer} to write.
+ * @return The number of bytes actually written.
+ *
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code buffer}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int write(byte[] buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+ /**
+ * Writes the entire contents of the {@link Pointer} {@code pointer} to
+ * this stream, starting at the current stream position.
+ *
+ * @param pointer
+ * The {@link Pointer} to write.
+ * @return The number of bytes actually written.
+ *
+ * @throws NullPointerException
+ * If {@code pointer} is {@code null}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long write(Pointer pointer)
+ throws NullPointerException, IOException;
+
+ /**
+ * Writes {@code count} bytes from the {@link Pointer} {@code pointer}
+ * to this stream, starting at the current stream position and
+ * using {@code offset} as the first position within
+ * {@code pointer} to get bytes.
+ *
+ * @param pointer
+ * The {@code Pointer} to write to this stream.
+ * @param offset
+ * The index of the first byte in {@code pointer} to write.
+ * @param count
+ * The number of bytes from {@code pointer} to write.
+ * @return The number of bytes actually written.
+ *
+ * @throws NullPointerException
+ * If {@code pointer} is {@code null}.
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code pointer}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long write(Pointer pointer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+ /**
+ * Writes the entire contents of the {@code ByteBuffer} {@code buffer}
+ * to this stream, starting at the current stream position.
+ * <p>
+ * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
+ * or obtained from {@link NioByteBuffer}.
+ * </p>
+ *
+ * @param buffer
+ * The {@code ByteBuffer} to write.
+ * @return The number of bytes actually written.
+ *
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int write(ByteBuffer buffer)
+ throws IOException;
+
+ /**
+ * Writes {@code count} bytes from the {@code ByteBuffer} {@code buffer}
+ * to this stream, starting at the current stream position and
+ * using {@code offset} as the first position within {@code buffer}
+ * to get bytes.
+ * <p>
+ * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
+ * or obtained from {@link NioByteBuffer}.
+ * </p>
+ *
+ * @param buffer
+ * The {@code ByteBuffer} to write to this stream.
+ * @param offset
+ * The index of the first byte in {@code buffer} to write.
+ * @param count
+ * The number of bytes from {@code buffer} to write.
+ * @return The number of bytes actually written.
+ *
+ * @throws IndexOutOfBoundsException
+ * If {@code offset < 0} or {@code count < 0}, or if
+ * {@code offset + count} is greater than the size of
+ * {@code buffer}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int write(ByteBuffer buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+ /**
+ * Writes {@code count} arrays from the array of byte arrays {@code array}
+ * to this stream, starting at the current stream pointer and using
+ * {@code offset} as the first position within {@code array} to get bytes.
+ *
+ * @param array
+ * The array of buffer arrays to write to this stream.
+ * @param offset
+ * The index of the first array in {@code array} to write.
+ * @param count
+ * The number of arrays from {@code array} to write.
+ *
+ * @throws IndexOutOfBoundsException
+ * if {@code count < 0}, {@code offset < 0} or
+ * {@code count + offset} is greater than the size of
+ * {@code array}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long write(byte[][] array, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+ /**
+ * Writes {@code count} arrays from the {@code ByteBuffer} {@code array}
+ * to this stream, starting at the current stream position and using
+ * {@code offset} as the first position within {@code array} to get bytes.
+ *
+ * @param array
+ * The {@code ByteBuffer} array to write to this stream.
+ * @param offset
+ * The index of the first array in {@code array} to write.
+ * @param count
+ * The number of arrays from {@code array} to write.
+ * @throws IndexOutOfBoundsException
+ * if {@code count < 0}, {@code offset < 0} or
+ * {@code count + offset} is greater than the size of
+ * {@code array}.
+ * @throws ClosedDescriptorException
+ * If this stream is closed.
+ * @throws OperationWouldBlockException
+ * If the stream is in nonblocking mode and the operation
+ * would block.
+ * @throws TimeoutException
+ * If write operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public long write(ByteBuffer[] array, int offset, int count)
+ throws IndexOutOfBoundsException, IOException;
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java?rev=1145784&view=auto
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
(added)
+++
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
Tue Jul 12 21:44:08 2011
@@ -0,0 +1,284 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime.net;
+
+import java.io.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.SyncFailedException;
+import java.net.SocketException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.io.ClosedDescriptorException;
+import org.apache.commons.runtime.io.Descriptor;
+import org.apache.commons.runtime.io.OperationInProgressException;
+import org.apache.commons.runtime.io.OperationWouldBlockException;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.TimeoutException;
+
+/**
+ * Socket stream implementation.
+ */
+class SocketStream extends Stream
+{
+ /**
+ * Native socket descriptor pointer
+ */
+ private long nd;
+ private Descriptor sd;
+ private static native long alloc0(long fd);
+ private static native void close0(long nd);
+
+ private SocketStream()
+ {
+ // No instance
+ }
+
+ /**
+ * Create new Stream object from the given Descriptor.
+ * The Descripor's reference counter is incremented on
+ * each operation meaning that the object finalize() won't
+ * release the object if still used.
+ *
+ * @param fd native Descriptor pointer
+ */
+ public SocketStream(Descriptor sd)
+ {
+ this.sd = sd;
+ nd = alloc0(sd.fd());
+ }
+
+ @Override
+ public final void close()
+ throws IOException
+ {
+ if (valid()) {
+ try {
+ close0(nd);
+ sd.close();
+ }
+ finally {
+ nd = 0L;
+ sd = null;
+ }
+ }
+ }
+
+ @Override
+ public long skip(long count)
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+
+ return 0L;
+ }
+
+ @Override
+ public boolean closed()
+ {
+ if (sd == null)
+ return true;
+ else
+ return sd.closed();
+ }
+
+ @Override
+ public boolean valid()
+ {
+ if (sd != null)
+ return sd.valid();
+ else
+ return false;
+ }
+
+ @Override
+ public void sync()
+ throws SyncFailedException, IOException
+ {
+ if (sd == null)
+ throw new ClosedDescriptorException();
+ sd.sync();
+ }
+
+ @Override
+ public void flush()
+ throws SyncFailedException, IOException
+ {
+ if (sd == null)
+ throw new ClosedDescriptorException();
+ sd.flush();
+ }
+
+ @Override
+ public boolean eof()
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+
+ return false;
+ }
+
+ // === Reader methods
+
+ @Override
+ public int available()
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return 0;
+ }
+
+ @Override
+ public int read()
+ throws IOException
+ {
+
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ @Override
+ public int read(byte[] buffer)
+ throws IOException
+ {
+
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ @Override
+ public int read(byte[] buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ @Override
+ public long read(Pointer pointer)
+ throws NullPointerException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return 0L;
+ }
+
+ @Override
+ public long read(Pointer pointer, long offset, long count)
+ throws NullPointerException, IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return 0L;
+ }
+
+ @Override
+ public int read(ByteBuffer buffer)
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public int read(ByteBuffer buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ // === Writer methods
+
+ public int write(int b)
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public int write(byte[] buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public long write(Pointer pointer)
+ throws NullPointerException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public long write(Pointer pointer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public int write(ByteBuffer buffer)
+ throws IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public int write(ByteBuffer buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return -1;
+ }
+
+ public long write(byte[][] array, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return 0L;
+ }
+
+ public long write(ByteBuffer[] array, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (closed())
+ throw new ClosedDescriptorException();
+ return 0L;
+ }
+
+}
Propchange:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c?rev=1145784&r1=1145783&r2=1145784&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/inetsock.c Tue Jul 12
21:44:08 2011
@@ -98,11 +98,13 @@ ACR_NET_EXPORT(jint, SocketDescriptor, c
if (fd == 0)
return ACR_EBADF;
- if (AcrAtomic32Dec(&fd->refs) == 0) {
+ if (fd->u.s != -1) {
if (r_close(fd->u.s) == -1)
rc = ACR_GET_OS_ERROR();
- AcrFree(fd);
+ fd->u.s = -1;
}
+ if (AcrAtomic32Dec(&fd->refs) == 0)
+ AcrFree(fd);
return rc;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c?rev=1145784&r1=1145783&r2=1145784&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Tue Jul 12
21:44:08 2011
@@ -40,11 +40,13 @@ ACR_NET_EXPORT(jint, LocalDescriptor, cl
if (fd == 0)
return ACR_EBADF;
- if (AcrAtomic32Dec(&fd->refs) == 0) {
+ if (fd->u.s != -1) {
if (r_close(fd->u.s) == -1)
rc = ACR_GET_OS_ERROR();
- AcrFree(fd);
+ fd->u.s = -1;
}
+ if (AcrAtomic32Dec(&fd->refs) == 0)
+ AcrFree(fd);
return rc;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c?rev=1145784&r1=1145783&r2=1145784&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/localsock.c Tue Jul
12 21:44:08 2011
@@ -53,12 +53,15 @@ ACR_NET_EXPORT(jint, LocalDescriptor, cl
if (wd == 0)
return ACR_EBADF;
- if (AcrAtomic32Dec(&wd->fd.refs) == 0) {
+ if (wd->fd.u.s != INVALID_SOCKET) {
SAFE_CLOSE_HANDLE(wd->fh);
+ wd->fh = 0;
if (closesocket(wd->fd.u.s) == SOCKET_ERROR)
rc = ACR_GET_NETOS_ERROR();
+ wd->fd.u.s = INVALID_SOCKET;
+ }
+ if (AcrAtomic32Dec(&wd->fd.refs) == 0)
AcrFree(wd);
- }
return rc;
}
@@ -289,7 +292,7 @@ ACR_NET_EXPORT(jlong, LocalServerEndpoin
memset(&sa, 0, sizeof(sa));
sd = accept(wd->fd.u.s, 0, 0);
- if (sd == SOCKET_ERROR) {
+ if (sd == INVALID_SOCKET) {
ACR_THROW_NET_ERRNO();
return 0;
}
Modified:
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java?rev=1145784&r1=1145783&r2=1145784&view=diff
==============================================================================
---
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
(original)
+++
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
Tue Jul 12 21:44:08 2011
@@ -29,6 +29,15 @@ public class TestMain extends Assert
System.out.flush();
}
+ @Test(groups = { "core" })
+ public void testOs()
+ {
+ System.out.println("Running on " + Os.getSysname() + "/" +
+ Os.getDataModel() + " with " +
+ Os.getNumCpu() + " cpu's");
+ System.out.flush();
+ }
+
@AfterSuite(groups = { "init" })
public void shutDown()
{