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