summaryrefslogtreecommitdiffstats
path: root/libjava/gnu/java/nio/FileChannelImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/gnu/java/nio/FileChannelImpl.java')
-rw-r--r--libjava/gnu/java/nio/FileChannelImpl.java123
1 files changed, 84 insertions, 39 deletions
diff --git a/libjava/gnu/java/nio/FileChannelImpl.java b/libjava/gnu/java/nio/FileChannelImpl.java
index c233d8210a7..79a7a58eb90 100644
--- a/libjava/gnu/java/nio/FileChannelImpl.java
+++ b/libjava/gnu/java/nio/FileChannelImpl.java
@@ -126,19 +126,15 @@ public class FileChannelImpl extends FileChannel
public int read (ByteBuffer dst) throws IOException
{
- int s = (int)size();
-
- if (buf == null)
+ // Check if file is mapped into memory.
+ if (buf != null)
{
- throw new EOFException("file not mapped");
+ // FIXME: implement this
+ throw new Error ("Accessing mapped buffers not implemented.");
}
- for (int i = 0; i < s; i++)
- {
- dst.put (buf.get());
- }
-
- return s;
+ // File not mapped, access it directly.
+ return implRead (dst);
}
public int read (ByteBuffer dst, long position)
@@ -149,11 +145,33 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
+
+ if (file_obj instanceof FileOutputStream)
+ throw new NonReadableChannelException ();
+
+ int result;
+ long oldPosition;
+
+ oldPosition = implPosition ();
+ result = implRead (dst);
+ implPosition (oldPosition);
- // FIXME: check for NonReadableChannelException
+ return result;
+ }
- throw new Error ("Not implemented");
+ private int implRead (ByteBuffer dst) throws IOException
+ {
+ int result;
+ byte[] buffer = new byte [dst.remaining ()];
+
+ result = implRead (buffer, 0, buffer.length);
+ dst.put (buffer, 0, result);
+
+ return result;
}
+
+ private native int implRead (byte[] buffer, int offset, int length)
+ throws IOException;
public long read (ByteBuffer[] dsts, int offset, int length)
throws IOException
@@ -162,7 +180,7 @@ public class FileChannelImpl extends FileChannel
for (int i = offset; i < offset + length; i++)
{
- result += write (dsts [i]);
+ result += read (dsts [i]);
}
return result;
@@ -170,20 +188,15 @@ public class FileChannelImpl extends FileChannel
public int write (ByteBuffer src) throws IOException
{
- int w = 0;
-
- if (buf == null)
- {
- throw new EOFException ("file not mapped");
- }
-
- while (src.hasRemaining ())
+ // Check if file is mapped into memory.
+ if (buf != null)
{
- buf.put (src.get ());
- w++;
+ // FIXME: implement this
+ throw new Error ("Accessing mapped buffers not implemented.");
}
-
- return w;
+
+ // File not mapped, access it directly.
+ return implWrite (src);
}
public int write (ByteBuffer src, long position)
@@ -195,10 +208,29 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
- // FIXME: check for NonWritableChannelException
+ if (file_obj instanceof FileInputStream)
+ throw new NonWritableChannelException ();
- throw new Error ("Not implemented");
+ int result;
+ long oldPosition;
+
+ oldPosition = implPosition ();
+ result = implWrite (src);
+ implPosition (oldPosition);
+
+ return result;
}
+
+ private int implWrite (ByteBuffer src) throws IOException
+ {
+ byte[] buffer = new byte [src.remaining ()];
+
+ src.get (buffer, 0, buffer.length);
+ return implWrite (buffer, 0, buffer.length);
+ }
+
+ private native int implWrite (byte[] buffer, int offset, int length)
+ throws IOException;
public long write(ByteBuffer[] srcs, int offset, int length)
throws IOException
@@ -225,6 +257,7 @@ public class FileChannelImpl extends FileChannel
|| size > Integer.MAX_VALUE)
throw new IllegalArgumentException ();
+ // FIXME: Make this working.
int cmode = mode.m;
map_address = nio_mmap_file (position, size, cmode);
length = (int) size;
@@ -272,10 +305,13 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
- // FIXME: check for NonReadableChannelException
- // FIXME: check for NonWritableChannelException
-
- throw new Error ("Not implemented");
+ if (file_obj instanceof FileOutputStream)
+ throw new NonReadableChannelException ();
+
+ // XXX: count needs to be casted from long to int. Dataloss ?
+ ByteBuffer buffer = ByteBuffer.allocate ((int) count);
+ read (buffer, position);
+ return target.write (buffer);
}
public long transferFrom (ReadableByteChannel src, long position, long count)
@@ -288,10 +324,13 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
- // FIXME: check for NonReadableChannelException
- // FIXME: check for NonWritableChannelException
-
- throw new Error ("Not implemented");
+ if (file_obj instanceof FileInputStream)
+ throw new NonWritableChannelException ();
+
+ // XXX: count needs to be casted from long to int. Dataloss ?
+ ByteBuffer buffer = ByteBuffer.allocate ((int) count);
+ src.read (buffer);
+ return write (buffer, position);
}
public FileLock lock (long position, long size, boolean shared)
@@ -304,9 +343,14 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
- // FIXME: check for NonReadableChannelException
- // FIXME: check for NonWritableChannelException
-
+ if (shared &&
+ file_obj instanceof FileOutputStream)
+ throw new NonReadableChannelException ();
+
+ if (!shared &&
+ file_obj instanceof FileInputStream)
+ throw new NonWritableChannelException ();
+
throw new Error ("Not implemented");
}
@@ -353,7 +397,8 @@ public class FileChannelImpl extends FileChannel
if (!isOpen ())
throw new ClosedChannelException ();
- // FIXME: check for NonWritableChannelException
+ if (file_obj instanceof FileInputStream)
+ throw new NonWritableChannelException ();
return implTruncate (size);
}
OpenPOWER on IntegriCloud