1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.stream;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.FileRegion;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.RandomAccessFile;
25
26
27
28
29
30
31
32
33 public class ChunkedFile implements ChunkedInput<ByteBuf> {
34
35 private final RandomAccessFile file;
36 private final long startOffset;
37 private final long endOffset;
38 private final int chunkSize;
39 private long offset;
40
41
42
43
44 public ChunkedFile(File file) throws IOException {
45 this(file, ChunkedStream.DEFAULT_CHUNK_SIZE);
46 }
47
48
49
50
51
52
53
54 public ChunkedFile(File file, int chunkSize) throws IOException {
55 this(new RandomAccessFile(file, "r"), chunkSize);
56 }
57
58
59
60
61 public ChunkedFile(RandomAccessFile file) throws IOException {
62 this(file, ChunkedStream.DEFAULT_CHUNK_SIZE);
63 }
64
65
66
67
68
69
70
71 public ChunkedFile(RandomAccessFile file, int chunkSize) throws IOException {
72 this(file, 0, file.length(), chunkSize);
73 }
74
75
76
77
78
79
80
81
82
83 public ChunkedFile(RandomAccessFile file, long offset, long length, int chunkSize) throws IOException {
84 if (file == null) {
85 throw new NullPointerException("file");
86 }
87 if (offset < 0) {
88 throw new IllegalArgumentException(
89 "offset: " + offset + " (expected: 0 or greater)");
90 }
91 if (length < 0) {
92 throw new IllegalArgumentException(
93 "length: " + length + " (expected: 0 or greater)");
94 }
95 if (chunkSize <= 0) {
96 throw new IllegalArgumentException(
97 "chunkSize: " + chunkSize +
98 " (expected: a positive integer)");
99 }
100
101 this.file = file;
102 this.offset = startOffset = offset;
103 endOffset = offset + length;
104 this.chunkSize = chunkSize;
105
106 file.seek(offset);
107 }
108
109
110
111
112 public long startOffset() {
113 return startOffset;
114 }
115
116
117
118
119 public long endOffset() {
120 return endOffset;
121 }
122
123
124
125
126 public long currentOffset() {
127 return offset;
128 }
129
130 @Override
131 public boolean isEndOfInput() throws Exception {
132 return !(offset < endOffset && file.getChannel().isOpen());
133 }
134
135 @Override
136 public void close() throws Exception {
137 file.close();
138 }
139
140 @Override
141 public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
142 long offset = this.offset;
143 if (offset >= endOffset) {
144 return null;
145 }
146
147 int chunkSize = (int) Math.min(this.chunkSize, endOffset - offset);
148
149
150 ByteBuf buf = ctx.alloc().heapBuffer(chunkSize);
151 boolean release = true;
152 try {
153 file.readFully(buf.array(), buf.arrayOffset(), chunkSize);
154 buf.writerIndex(chunkSize);
155 this.offset = offset + chunkSize;
156 release = false;
157 return buf;
158 } finally {
159 if (release) {
160 buf.release();
161 }
162 }
163 }
164 }