1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel;
17
18 import io.netty.util.AbstractReferenceCounted;
19 import io.netty.util.IllegalReferenceCountException;
20 import io.netty.util.internal.ObjectUtil;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.channels.FileChannel;
28 import java.nio.channels.WritableByteChannel;
29
30 import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
31
32
33
34
35
36
37
38 public class DefaultFileRegion extends AbstractReferenceCounted implements FileRegion {
39
40 private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultFileRegion.class);
41 private final File f;
42 private final long position;
43 private final long count;
44 private long transferred;
45 private FileChannel file;
46
47
48
49
50
51
52
53
54 public DefaultFileRegion(FileChannel fileChannel, long position, long count) {
55 this.file = ObjectUtil.checkNotNull(fileChannel, "fileChannel");
56 this.position = checkPositiveOrZero(position, "position");
57 this.count = checkPositiveOrZero(count, "count");
58 this.f = null;
59 }
60
61
62
63
64
65
66
67
68
69 public DefaultFileRegion(File file, long position, long count) {
70 this.f = ObjectUtil.checkNotNull(file, "file");
71 this.position = checkPositiveOrZero(position, "position");
72 this.count = checkPositiveOrZero(count, "count");
73 }
74
75
76
77
78 public boolean isOpen() {
79 return file != null;
80 }
81
82 FileChannel fileChannel() {
83 return file;
84 }
85
86
87
88
89 public void open() throws IOException {
90 if (!isOpen() && refCnt() > 0) {
91
92 file = new RandomAccessFile(f, "r").getChannel();
93 }
94 }
95
96 @Override
97 public long position() {
98 return position;
99 }
100
101 @Override
102 public long count() {
103 return count;
104 }
105
106 @Deprecated
107 @Override
108 public long transfered() {
109 return transferred;
110 }
111
112 @Override
113 public long transferred() {
114 return transferred;
115 }
116
117 @Override
118 public long transferTo(WritableByteChannel target, long position) throws IOException {
119 long count = this.count - position;
120 if (count < 0 || position < 0) {
121 throw new IllegalArgumentException(
122 "position out of range: " + position +
123 " (expected: 0 - " + (this.count - 1) + ')');
124 }
125 if (count == 0) {
126 return 0L;
127 }
128 if (refCnt() == 0) {
129 throw new IllegalReferenceCountException(0);
130 }
131
132 open();
133
134 long written = file.transferTo(this.position + position, count, target);
135 if (written > 0) {
136 transferred += written;
137 } else if (written == 0) {
138
139
140
141
142 validate(this, position);
143 }
144 return written;
145 }
146
147 @Override
148 protected void deallocate() {
149 FileChannel file = this.file;
150
151 if (file == null) {
152 return;
153 }
154 this.file = null;
155
156 try {
157 file.close();
158 } catch (IOException e) {
159 logger.warn("Failed to close a file.", e);
160 }
161 }
162
163 @Override
164 public FileRegion retain() {
165 super.retain();
166 return this;
167 }
168
169 @Override
170 public FileRegion retain(int increment) {
171 super.retain(increment);
172 return this;
173 }
174
175 @Override
176 public FileRegion touch() {
177 return this;
178 }
179
180 @Override
181 public FileRegion touch(Object hint) {
182 return this;
183 }
184
185 static void validate(DefaultFileRegion region, long position) throws IOException {
186
187
188
189
190 long size = region.file.size();
191 long count = region.count - position;
192 if (region.position + count + position > size) {
193 throw new IOException("Underlying file size " + size + " smaller then requested count " + region.count);
194 }
195 }
196 }