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