Skip to content

Commit f7d0c48

Browse files
committed
[Feature] Add FileChannels.from().
1 parent f5a24b0 commit f7d0c48

File tree

3 files changed

+232
-2
lines changed

3 files changed

+232
-2
lines changed

generate.sh

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ import java8.nio.file.StandardOpenOption;
6666
import java8.nio.file.attribute.FileAttribute;
6767
import 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
}
253254
EOF
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
254370
find "${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;"

library/src/main/java/java8/nio/channels/FileChannel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
import java8.nio.file.attribute.FileAttribute;
3737
import java8.nio.file.spi.FileSystemProvider;
3838

39-
public abstract class FileChannel extends java.nio.channels.FileChannel {
39+
public abstract class FileChannel extends java.nio.channels.FileChannel
40+
implements SeekableByteChannel {
4041

4142
protected FileChannel() {}
4243

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2018 Hai Zhang <dreaming.in.code.zh@gmail.com>
3+
* All Rights Reserved.
4+
*/
5+
6+
package java8.nio.channels;
7+
8+
import java.io.IOException;
9+
import java.nio.ByteBuffer;
10+
import java.nio.MappedByteBuffer;
11+
import java.nio.channels.FileLock;
12+
import java.nio.channels.ReadableByteChannel;
13+
import java.nio.channels.WritableByteChannel;
14+
15+
public class FileChannels {
16+
17+
private FileChannels() {}
18+
19+
public static FileChannel from(java.nio.channels.FileChannel fileChannel) {
20+
return new DelegateFileChannel(fileChannel);
21+
}
22+
23+
private static class DelegateFileChannel extends FileChannel {
24+
25+
private final java.nio.channels.FileChannel mFileChannel;
26+
27+
public DelegateFileChannel(java.nio.channels.FileChannel fileChannel) {
28+
mFileChannel = fileChannel;
29+
}
30+
31+
@Override
32+
public int read(ByteBuffer dst) throws IOException {
33+
return mFileChannel.read(dst);
34+
}
35+
36+
@Override
37+
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
38+
return mFileChannel.read(dsts, offset, length);
39+
}
40+
41+
@Override
42+
public int write(ByteBuffer src) throws IOException {
43+
return mFileChannel.write(src);
44+
}
45+
46+
@Override
47+
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
48+
return mFileChannel.write(srcs, offset, length);
49+
}
50+
51+
@Override
52+
public long position() throws IOException {
53+
return mFileChannel.position();
54+
}
55+
56+
@Override
57+
public DelegateFileChannel position(long newPosition) throws IOException {
58+
mFileChannel.position(newPosition);
59+
return this;
60+
}
61+
62+
@Override
63+
public long size() throws IOException {
64+
return mFileChannel.size();
65+
}
66+
67+
@Override
68+
public DelegateFileChannel truncate(long size) throws IOException {
69+
mFileChannel.truncate(size);
70+
return this;
71+
}
72+
73+
public void force(boolean metaData) throws IOException {
74+
mFileChannel.force(metaData);
75+
}
76+
77+
public long transferTo(long position, long count, WritableByteChannel target)
78+
throws IOException {
79+
return mFileChannel.transferTo(position, count, target);
80+
}
81+
82+
public long transferFrom(ReadableByteChannel src, long position, long count)
83+
throws IOException {
84+
return mFileChannel.transferFrom(src, position, count);
85+
}
86+
87+
public int read(ByteBuffer dst, long position) throws IOException {
88+
return mFileChannel.read(dst, position);
89+
}
90+
91+
public int write(ByteBuffer src, long position) throws IOException {
92+
return mFileChannel.write(src, position);
93+
}
94+
95+
public MappedByteBuffer map(MapMode mode, long position, long size)
96+
throws IOException {
97+
return mFileChannel.map(mode, position, size);
98+
}
99+
100+
public FileLock lock(long position, long size, boolean shared) throws IOException {
101+
return mFileChannel.lock(position, size, shared);
102+
}
103+
104+
public FileLock tryLock(long position, long size, boolean shared) throws IOException {
105+
return mFileChannel.tryLock(position, size, shared);
106+
}
107+
108+
@Override
109+
protected void implCloseChannel() throws IOException {
110+
mFileChannel.close();
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)