summaryrefslogtreecommitdiff
path: root/gnu/java/nio/channels/FileChannelImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/java/nio/channels/FileChannelImpl.java')
-rw-r--r--gnu/java/nio/channels/FileChannelImpl.java98
1 files changed, 65 insertions, 33 deletions
diff --git a/gnu/java/nio/channels/FileChannelImpl.java b/gnu/java/nio/channels/FileChannelImpl.java
index 664e93c87..0ee5d3fc2 100644
--- a/gnu/java/nio/channels/FileChannelImpl.java
+++ b/gnu/java/nio/channels/FileChannelImpl.java
@@ -15,8 +15,8 @@ General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
-Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-02111-1307 USA.
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
@@ -73,6 +73,10 @@ public final class FileChannelImpl extends FileChannel
public static final int SYNC = 16;
public static final int DSYNC = 32;
+ public static FileChannelImpl in;
+ public static FileChannelImpl out;
+ public static FileChannelImpl err;
+
private static native void init();
static
@@ -83,6 +87,10 @@ public final class FileChannelImpl extends FileChannel
}
init();
+
+ in = new FileChannelImpl(0, READ);
+ out = new FileChannelImpl(1, WRITE);
+ err = new FileChannelImpl(2, WRITE);
}
/**
@@ -97,6 +105,8 @@ public final class FileChannelImpl extends FileChannel
private int mode;
+ final String description;
+
/* Open a file. MODE is a combination of the above mode flags. */
/* This is a static factory method, so that VM implementors can decide
* substitute subclasses of FileChannelImpl. */
@@ -109,7 +119,8 @@ public final class FileChannelImpl extends FileChannel
private FileChannelImpl(File file, int mode)
throws FileNotFoundException
{
- final String path = file.getPath();
+ String path = file.getPath();
+ description = path;
fd = open (path, mode);
this.mode = mode;
@@ -126,21 +137,26 @@ public final class FileChannelImpl extends FileChannel
/* ignore it */
}
- throw new FileNotFoundException(path + " is a directory");
+ throw new FileNotFoundException(description + " is a directory");
}
}
- /* Used by init() (native code) */
+ /**
+ * Constructor for default channels in, out and err.
+ *
+ * Used by init() (native code).
+ *
+ * @param fd the file descriptor (0, 1, 2 for stdin, stdout, stderr).
+ *
+ * @param mode READ or WRITE
+ */
FileChannelImpl (int fd, int mode)
{
this.fd = fd;
this.mode = mode;
+ this.description = "descriptor(" + fd + ")";
}
- public static FileChannelImpl in;
- public static FileChannelImpl out;
- public static FileChannelImpl err;
-
private native int open (String path, int mode) throws FileNotFoundException;
public native int available () throws IOException;
@@ -179,7 +195,7 @@ public final class FileChannelImpl extends FileChannel
throws IOException
{
if (position < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position);
long oldPosition = implPosition ();
position (position);
int result = read(dst);
@@ -230,7 +246,7 @@ public final class FileChannelImpl extends FileChannel
throws IOException
{
if (position < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position);
if (!isOpen ())
throw new ClosedChannelException ();
@@ -288,10 +304,11 @@ public final class FileChannelImpl extends FileChannel
throw new NonWritableChannelException();
}
else
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("mode: " + mode);
if (position < 0 || size < 0 || size > Integer.MAX_VALUE)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position
+ + ", size: " + size);
return mapImpl(nmode, position, (int) size);
}
@@ -302,8 +319,12 @@ public final class FileChannelImpl extends FileChannel
{
if (!isOpen ())
throw new ClosedChannelException ();
+
+ force ();
}
+ private native void force ();
+
// like transferTo, but with a count of less than 2Gbytes
private int smallTransferTo (long position, int count,
WritableByteChannel target)
@@ -332,7 +353,8 @@ public final class FileChannelImpl extends FileChannel
{
if (position < 0
|| count < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position
+ + ", count: " + count);
if (!isOpen ())
throw new ClosedChannelException ();
@@ -395,7 +417,8 @@ public final class FileChannelImpl extends FileChannel
{
if (position < 0
|| count < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position
+ + ", count: " + count);
if (!isOpen ())
throw new ClosedChannelException ();
@@ -420,24 +443,31 @@ public final class FileChannelImpl extends FileChannel
return total;
}
- public FileLock tryLock (long position, long size, boolean shared)
+ // Shared sanity checks between lock and tryLock methods.
+ private void lockCheck(long position, long size, boolean shared)
throws IOException
{
if (position < 0
|| size < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("position: " + position
+ + ", size: " + size);
if (!isOpen ())
- throw new ClosedChannelException ();
+ throw new ClosedChannelException();
- if (shared && (mode & READ) == 0)
- throw new NonReadableChannelException ();
-
- if (!shared && (mode & WRITE) == 0)
- throw new NonWritableChannelException ();
+ if (shared && ((mode & READ) == 0))
+ throw new NonReadableChannelException();
+ if (!shared && ((mode & WRITE) == 0))
+ throw new NonWritableChannelException();
+ }
+
+ public FileLock tryLock (long position, long size, boolean shared)
+ throws IOException
+ {
+ lockCheck(position, size, shared);
+
boolean completed = false;
-
try
{
begin();
@@ -464,15 +494,9 @@ public final class FileChannelImpl extends FileChannel
public FileLock lock (long position, long size, boolean shared)
throws IOException
{
- if (position < 0
- || size < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
+ lockCheck(position, size, shared);
boolean completed = false;
-
try
{
boolean lockable = lock(position, size, shared, true);
@@ -500,7 +524,7 @@ public final class FileChannelImpl extends FileChannel
throws IOException
{
if (newPosition < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("newPostition: " + newPosition);
if (!isOpen ())
throw new ClosedChannelException ();
@@ -515,7 +539,7 @@ public final class FileChannelImpl extends FileChannel
throws IOException
{
if (size < 0)
- throw new IllegalArgumentException ();
+ throw new IllegalArgumentException ("size: " + size);
if (!isOpen ())
throw new ClosedChannelException ();
@@ -528,4 +552,12 @@ public final class FileChannelImpl extends FileChannel
return this;
}
+
+ public String toString()
+ {
+ return (this.getClass()
+ + "[fd=" + fd
+ + ",mode=" + mode + ","
+ + description + "]");
+ }
}