@@ -66,7 +66,8 @@ import java8.nio.file.StandardOpenOption;
6666import java8.nio.file.attribute.FileAttribute;
6767import java8.nio.file.spi.FileSystemProvider;
6868
69- public abstract class FileChannel extends java.nio.channels.FileChannel {
69+ public abstract class FileChannel extends java.nio.channels.FileChannel
70+ implements SeekableByteChannel {
7071
7172 protected FileChannel() {}
7273
@@ -251,6 +252,121 @@ public abstract class FileChannel extends java.nio.channels.FileChannel {
251252 public abstract FileChannel truncate(long size) throws IOException;
252253}
253254EOF
255+ cat > " ${LIBRARY_JAVA_SOURCE_ROOT} /java8/nio/channels/FileChannels.java" << EOF
256+ /*
257+ * Copyright (c) 2018 Hai Zhang <dreaming.in.code.zh@gmail.com>
258+ * All Rights Reserved.
259+ */
260+
261+ package java8.nio.channels;
262+
263+ import java.io.IOException;
264+ import java.nio.ByteBuffer;
265+ import java.nio.MappedByteBuffer;
266+ import java.nio.channels.FileLock;
267+ import java.nio.channels.ReadableByteChannel;
268+ import java.nio.channels.WritableByteChannel;
269+
270+ public class FileChannels {
271+
272+ private FileChannels() {}
273+
274+ public static FileChannel from(java.nio.channels.FileChannel fileChannel) {
275+ return new DelegateFileChannel(fileChannel);
276+ }
277+
278+ private static class DelegateFileChannel extends FileChannel {
279+
280+ private final java.nio.channels.FileChannel mFileChannel;
281+
282+ public DelegateFileChannel(java.nio.channels.FileChannel fileChannel) {
283+ mFileChannel = fileChannel;
284+ }
285+
286+ @Override
287+ public int read(ByteBuffer dst) throws IOException {
288+ return mFileChannel.read(dst);
289+ }
290+
291+ @Override
292+ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
293+ return mFileChannel.read(dsts, offset, length);
294+ }
295+
296+ @Override
297+ public int write(ByteBuffer src) throws IOException {
298+ return mFileChannel.write(src);
299+ }
300+
301+ @Override
302+ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
303+ return mFileChannel.write(srcs, offset, length);
304+ }
305+
306+ @Override
307+ public long position() throws IOException {
308+ return mFileChannel.position();
309+ }
310+
311+ @Override
312+ public DelegateFileChannel position(long newPosition) throws IOException {
313+ mFileChannel.position(newPosition);
314+ return this;
315+ }
316+
317+ @Override
318+ public long size() throws IOException {
319+ return mFileChannel.size();
320+ }
321+
322+ @Override
323+ public DelegateFileChannel truncate(long size) throws IOException {
324+ mFileChannel.truncate(size);
325+ return this;
326+ }
327+
328+ public void force(boolean metaData) throws IOException {
329+ mFileChannel.force(metaData);
330+ }
331+
332+ public long transferTo(long position, long count, WritableByteChannel target)
333+ throws IOException {
334+ return mFileChannel.transferTo(position, count, target);
335+ }
336+
337+ public long transferFrom(ReadableByteChannel src, long position, long count)
338+ throws IOException {
339+ return mFileChannel.transferFrom(src, position, count);
340+ }
341+
342+ public int read(ByteBuffer dst, long position) throws IOException {
343+ return mFileChannel.read(dst, position);
344+ }
345+
346+ public int write(ByteBuffer src, long position) throws IOException {
347+ return mFileChannel.write(src, position);
348+ }
349+
350+ public MappedByteBuffer map(MapMode mode, long position, long size)
351+ throws IOException {
352+ return mFileChannel.map(mode, position, size);
353+ }
354+
355+ public FileLock lock(long position, long size, boolean shared) throws IOException {
356+ return mFileChannel.lock(position, size, shared);
357+ }
358+
359+ public FileLock tryLock(long position, long size, boolean shared) throws IOException {
360+ return mFileChannel.tryLock(position, size, shared);
361+ }
362+
363+ @Override
364+ protected void implCloseChannel() throws IOException {
365+ mFileChannel.close();
366+ }
367+ }
368+ }
369+ EOF
254370find " ${LIBRARY_JAVA_SOURCE_ROOT} /java8/nio/file" -iname ' *.java' -type f -print0 | xargs -0 sed -Ei \
255371-e " s/\bjava(\.nio\.channels\.(File|SeekableByte)Channel)\b/java8\1/g" \
256372-e " /^\s*import\s+java\.nio\.channels\.\*\s*;\s*$/a\import java8.nio.channels.FileChannel;\nimport java8.nio.channels.SeekableByteChannel;"
0 commit comments