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