1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.ftp;
18
19 import java.io.DataInputStream;
20 import java.io.FilterInputStream;
21 import java.io.IOException;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.vfs2.FileSystemException;
25 import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
26 import org.apache.commons.vfs2.util.RandomAccessMode;
27
28
29
30
31 final class FtpRandomAccessContent extends AbstractRandomAccessStreamContent {
32
33 protected long filePointer;
34
35 private final FtpFileObject fileObject;
36 private DataInputStream dis;
37 private FtpFileObject.FtpInputStream mis;
38
39 FtpRandomAccessContent(final FtpFileObject fileObject, final RandomAccessMode mode) {
40 super(mode);
41
42 this.fileObject = fileObject;
43
44 }
45
46 @Override
47 public void close() throws IOException {
48 if (dis != null) {
49 mis.abort();
50
51
52 final DataInputStream oldDis = dis;
53 dis = null;
54 oldDis.close();
55 mis = null;
56 }
57 }
58
59 @Override
60 protected DataInputStream getDataInputStream() throws IOException {
61 if (dis != null) {
62 return dis;
63 }
64
65
66 mis = fileObject.getInputStream(filePointer);
67 dis = new DataInputStream(new FilterInputStream(mis) {
68 @Override
69 public void close() throws IOException {
70 FtpRandomAccessContent.this.close();
71 }
72
73 @Override
74 public int read() throws IOException {
75 final int ret = super.read();
76 if (ret > -1) {
77 filePointer++;
78 }
79 return ret;
80 }
81
82 @Override
83 public int read(final byte[] b) throws IOException {
84 final int ret = super.read(b);
85 if (ret > -1) {
86 filePointer += ret;
87 }
88 return ret;
89 }
90
91 @Override
92 public int read(final byte[] b, final int off, final int len) throws IOException {
93 final int ret = super.read(b, off, len);
94 if (ret > -1) {
95 filePointer += ret;
96 }
97 return ret;
98 }
99 });
100
101 return dis;
102 }
103
104 @Override
105 public long getFilePointer() throws IOException {
106 return filePointer;
107 }
108
109 @Override
110 public long length() throws IOException {
111 return fileObject.getContent().getSize();
112 }
113
114 @Override
115 public void seek(final long pos) throws IOException {
116 if (pos == filePointer) {
117
118 return;
119 }
120 if (pos < 0) {
121 throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
122 }
123 IOUtils.close(dis);
124 filePointer = pos;
125 }
126 }