You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/03 10:04:01 UTC
svn commit: r821265 - in /commons/sandbox/runtime/trunk: ./
src/main/java/org/apache/commons/runtime/exception/
src/main/java/org/apache/commons/runtime/io/ src/main/native/include/
src/main/native/os/unix/ src/main/native/os/win32/ src/main/native/sha...
Author: mturk
Date: Sat Oct 3 08:04:00 2009
New Revision: 821265
URL: http://svn.apache.org/viewvc?rev=821265&view=rev
Log:
Add TimeoutException
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java (with props)
Modified:
commons/sandbox/runtime/trunk/build.xml
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java
commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
commons/sandbox/runtime/trunk/src/main/native/shared/error.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
Modified: commons/sandbox/runtime/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/build.xml?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/build.xml (original)
+++ commons/sandbox/runtime/trunk/build.xml Sat Oct 3 08:04:00 2009
@@ -129,7 +129,7 @@
author="true"
version="true"
overview="${src.dir}/main/java/overview.html"
- packagenames="{build.package.name}.*"
+ packagenames="${build.package.name}.*"
windowtitle="${title} (Version ${version})"
doctitle="<h2>${title}</h2>"
bottom="Copyright 2009 The Apache Software Foundation<!--
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java?rev=821265&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java Sat Oct 3 08:04:00 2009
@@ -0,0 +1,42 @@
+/* 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.exception;
+import java.io.IOException;
+
+/**
+ * Exception thrown when a blocking operation times out. Blocking
+ * operations for which a timeout is specified need a means to
+ * indicate that the timeout has occurred. For many such operations it
+ * is possible to return a value that indicates timeout; when that is
+ * not possible or desirable then {@code TimeoutException} should be
+ * declared and thrown.
+ *
+ * @since Runtime 1.0
+ */
+public class TimeoutException extends IOException
+{
+
+ public TimeoutException()
+ {
+ super();
+ }
+
+ public TimeoutException(String msg)
+ {
+ super(msg);
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/exception/TimeoutException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/RandomAccessFile.java Sat Oct 3 08:04:00 2009
@@ -44,6 +44,15 @@
private Descriptor fd;
/**
+ * Return the {@link Descriptor} accosicated with this file.
+ *
+ * @return file's Descriptor object
+ */
+ public Descriptor fd()
+ {
+ return fd;
+ }
+ /**
* Close this file.
* @see java.io.Closeable#close()
* @throws IOException if an I/O error occurs.
@@ -55,9 +64,9 @@
}
/**
- * Flush the underlying file by writing any buffered data.
+ * Flush the underlying file metadata.
* <p>
- * {@code fsync()} transfers all modified in-core data of the file object
+ * {@code flush} transfers all modified metadata of the file object
* referred to by {@code this} file to the disk device
* (or other permanent storage device) where that object resides.
* The call blocks until the device reports that the transfer has
@@ -74,8 +83,28 @@
fd.flush();
}
+ /**
+ * Sync the underlying file by writing any buffered data.
+ * <p>
+ * {@code sync} transfers all modified in-core data of the file object
+ * referred to by {@code this} file to the disk device
+ * (or other permanent 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 final void sync()
+ throws SyncFailedException, IOException
+ {
+ fd.sync();
+ }
+
public RandomAccessFile(File file, EnumSet<FileOpenMode> mode)
- throws FileNotFoundException, IllegalArgumentException, IOException,
+ throws FileNotFoundException, IOException, IllegalArgumentException,
SecurityException
{
fd = FileWrapper.open(file, mode);
@@ -87,7 +116,7 @@
public RandomAccessFile(File file, EnumSet<FileOpenMode> mode,
EnumSet<FileProtection> prot)
- throws FileNotFoundException, IllegalArgumentException, IOException,
+ throws FileNotFoundException, IOException, IllegalArgumentException,
SecurityException
{
fd = FileWrapper.open(file, mode, prot);
@@ -104,5 +133,67 @@
this.fd = fd;
}
+ /**
+ * Reads a single byte from the current position in this file and returns
+ * it as an integer in the range from 0 to 255. Returns {@code -1} if the
+ * end of the file has been reached. Blocks until one byte has been read,
+ * the end of the file is detected or an exception is thrown.
+ *
+ * @return The byte read or {@code -1} if the end of the file has been reached.
+ * @throws ClosedDescriptorException
+ * If this file is closed.
+ * @throws AsyncClosedDescriptorException
+ * If another thread closes this file while the read
+ * operation is in progress.
+ * @throws TimeoutException
+ * If read operation times out.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read()
+ throws IndexOutOfBoundsException, IOException
+ {
+ return FileWrapper.read(fd);
+ }
+ /**
+ * Reads at most {@code count} bytes from the current position in this file
+ * 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 file is reached or an exception is thrown.
+ *
+ * @param buffer
+ * The array in which to store the bytes read from this file.
+ * @param offset
+ * The initial position in {@code buffer} to store the bytes read
+ * from this file.
+ * @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 file is closed.
+ * @throws AsyncClosedDescriptorException
+ * If another thread closes this file while the read
+ * operation is in progress.
+ * @throws IOException
+ * If some other I/O error occurs.
+ */
+ public int read(byte[] buffer, int offset, int count)
+ throws IndexOutOfBoundsException, IOException
+ {
+ if (count > (buffer.length - offset) || count < 0 || offset < 0)
+ throw new IndexOutOfBoundsException();
+ if (count == 0) {
+ /* Returning zero usually represents a timeout.
+ */
+ return 0;
+ }
+ return FileWrapper.read(fd, buffer, offset, count);
+ }
}
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Sat Oct 3 08:04:00 2009
@@ -59,6 +59,7 @@
ACR_EX_INVALID_DESC, /* exception/InvalidDescriptorException */
ACR_EX_ENOENT, /* exception/NoSuchObjectException */
ACR_EX_OSERR, /* exception/OperatingSystemException */
+ ACR_EX_ETIMEOUT, /* exception/TimeoutException */
ACR_EX_UNALIGNED, /* exception/UnalignedMemoryException */
ACR_EX_UNSUPPORTED, /* exception/UnsupportedOperatingSystemException */
ACR_EX_LEN
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c Sat Oct 3 08:04:00 2009
@@ -732,6 +732,7 @@
switch (rc) {
case 0:
case ACR_TIMEUP:
+ ACR_THROW_EX_IF_ERR(ACR_EX_ETIMEOUT, rc);
break;
case EBADF:
ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c Sat Oct 3 08:04:00 2009
@@ -898,7 +898,7 @@
case 0:
case ACR_TIMEUP:
case ERROR_OPERATION_ABORTED:
- return 0;
+ ACR_THROW_EX_IF_ERR(ACR_EX_ETIMEOUT, rc);
break;
case ERROR_INVALID_HANDLE:
ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Sat Oct 3 08:04:00 2009
@@ -46,6 +46,7 @@
ACR_EXCEPTION_CLASS_PATH "InvalidDescriptorException",
ACR_EXCEPTION_CLASS_PATH "NoSuchObjectException",
ACR_EXCEPTION_CLASS_PATH "OperatingSystemException",
+ ACR_EXCEPTION_CLASS_PATH "TimeoutException",
ACR_EXCEPTION_CLASS_PATH "UnalignedMemoryException",
ACR_EXCEPTION_CLASS_PATH "UnsupportedOperatingSystemException",
NULL
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java?rev=821265&r1=821264&r2=821265&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Sat Oct 3 08:04:00 2009
@@ -21,6 +21,7 @@
import org.apache.commons.runtime.util.*;
import junit.framework.*;
import java.io.IOException;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.EnumSet;
@@ -46,21 +47,28 @@
throws Exception
{
File file = new File("ftest1.txt");
- Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE));
+ RandomAccessFile f = new RandomAccessFile(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE));
- assertFalse("Descriptor", fd == null);
+ assertFalse("RandomAccessFile", f == null);
System.out.println();
- assertEquals("Descriptor type", DescriptorType.FILE, fd.getType());
- fd.close();
+ f.close();
}
public void testFileSysOpenExisting()
throws Exception
{
File file = new File("ftest1.txt");
- Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
- assertFalse("Descriptor", fd != null);
-
+ try {
+ RandomAccessFile f = new RandomAccessFile(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ assertFalse("RandomAccessFile", f != null);
+ f.close();
+ }
+ catch (FileNotFoundException fe) {
+ // OK
+ }
+ catch (Exception ex) {
+ fail("Wrong exception " + ex);
+ }
}
public void testFileSysOpenAgain()
@@ -68,9 +76,9 @@
{
File file = new File("ftest1.txt");
file.delete();
- Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
- assertFalse("Descriptor", fd == null);
- fd.close();
+ RandomAccessFile f = new RandomAccessFile(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ assertFalse("RandomAccessFile", f == null);
+ f.close();
file.delete();
}
@@ -83,16 +91,16 @@
file.delete();
file.createNewFile();
FileOutputStream os = new FileOutputStream(file);
-
+
byte [] rnd = new byte[1024];
while (len > 0) {
- Utils.generateRandom(rnd);
+ Utils.generateRandom(rnd);
int siz = rnd.length;
if (siz > len)
siz = len;
os.write(rnd, 0, siz);
- len -= siz;
- }
+ len -= siz;
+ }
os.close();
}
@@ -101,15 +109,15 @@
{
File file = new File("ftest1.rnd");
makeRandomFile(file.getPath(), 1024 * 128);
-
- Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.READ, FileOpenMode.NONBLOCK));
- assertFalse("Descriptor", fd == null);
+
+ RandomAccessFile f = new RandomAccessFile(file, EnumSet.of(FileOpenMode.READ, FileOpenMode.NONBLOCK));
+ assertFalse("RandomAccessFile", f == null);
byte [] buf = new byte[1024 * 128];
-
- int rd = FileSystem.read(fd, buf, 0, buf.length);
+
+ int rd = f.read(buf, 0, buf.length);
System.out.println();
System.out.println("Readed " + rd + " bytes from " + file.getPath());
- fd.close();
+ f.close();
}
}