Source code: com/yaftp/ftp/FtpVectorListener.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.ftp ;
21
22 import java.util.* ;
23 import java.io.* ;
24
25 /**
26
27 provide a standard String vector for incoming String DataList
28
29 @Author Jean-Yves MENGANT
30 @version : 0.0.1
31
32 */
33
34 public class FtpVectorListener
35 extends FtpStandardListener {
36
37 /**
38 rfc 765 indicates that a CRLF should be sent as a end of line
39 <CRLF> combination should be used where necessary to denote the
40 end of a line
41 */
42
43 private final static String _CRLF_ = "\r\n" ;
44 private Vector _dataList ; // Vector of transmitted items
45
46 private BufferedWriter _localWriter ;
47 private BufferedReader _localReader ;
48
49 /**
50 call this method whenever you want to ftp PUT from a local file
51 rather than by buffer
52 when a _localReader has been built the writeData will be read from
53 _localReader rather than from _dataList
54 */
55 public void buildLocalReader( File file )
56 throws ClientFtpError
57 {
58 try {
59 _localReader = new BufferedReader(
60 new FileReader( file )
61 ) ;
62 } catch ( IOException e )
63 { throw new ClientFtpError("buildLocalReader error : " + e.getMessage() ) ; }
64 }
65
66 public void closeLocalReader()
67 throws ClientFtpError
68 {
69 try {
70 if ( _localReader != null )
71 _localReader.close();
72 } catch ( IOException e )
73 { throw new ClientFtpError("closeLocalReader error : " + e.getMessage() ) ; }
74 }
75
76 /**
77 call this method whenever you want to ftp GET intro a local file
78 rather than by buffer
79 when a _localReader has been built the writeData will be read from
80 _localReader rather than from _dataList
81 */
82 public void buildLocalWriter( File file )
83 throws ClientFtpError
84 {
85 try {
86 _localWriter = new BufferedWriter(
87 new FileWriter( file )
88 ) ;
89 } catch ( IOException e )
90 { throw new ClientFtpError("buildLocalWriter error : " + e.getMessage() ) ; }
91 }
92
93 public void closeLocalWriter()
94 throws ClientFtpError
95 {
96 try {
97 if ( _localWriter != null )
98 _localWriter.close();
99 } catch ( IOException e )
100 { throw new ClientFtpError("closeLocalWriter error : " + e.getMessage() ) ; }
101 }
102
103 //
104 // FtpClientSession public methods follow
105 //
106 // any result from DATA SOCKET is given back here
107 // ( override this method if you need a diferent container than Vector
108 //
109 public void readData( InputStream myStream ,
110 FtpDataNotifier dataEvt
111 )
112 {
113 // System.out.println( "Entering read Data callback " ) ;
114 if ( _localWriter == null )
115 _dataList = new Vector(20,20) ;
116 FtpBufferTransfertEvent bufferEvent = null ;
117 if ( _listener != null )
118 bufferEvent = new FtpBufferTransfertEvent( this , 0 ) ;
119
120 try {
121 BufferedReader myReader = new BufferedReader (
122 new InputStreamReader( myStream )
123 ) ;
124 try {
125 String str = myReader.readLine() ;
126 while ( str != null )
127 {
128 // write on local datalist if local writer has not been allocated
129 if ( _dataList != null )
130 _dataList.addElement( str ) ;
131 if ( _localWriter != null )
132 _localWriter.write(str+ _CRLF_ );
133
134 str = myReader.readLine() ;
135 // brodcast a FileBufferTransfertEvent to any registered listener
136 if ( str != null )
137 broadcastBufferEvent(str.length()) ;
138 }
139 if ( _localWriter != null )
140 _localWriter.flush() ;
141 } catch ( EOFException e ) {} // just end loop
142 } catch ( IOException e ) {
143 dataError( " Error readind data " + e.toString() , dataEvt ) ;
144 }
145
146 // System.out.println("Notify for nb reads : " + _dataList.size() ) ;
147 // notify waiting thread about data availability
148 if ( dataEvt != null )
149 dataEvt.makeDataVisible() ;
150 // System.out.println( "exiting read Data callBack" ) ;
151 }
152
153
154 private void writeFromBuffer( OutputStream myStream , FtpDataNotifier dataEvt )
155 {
156 // System.out.println("entering write Data callBack") ;
157 if ( (_dataList != null ) && ( ! _dataList.isEmpty() ) )
158 {
159 // Check vector Data type is String and upload to Ftp server
160 if ( _dataList.firstElement() instanceof String )
161 {
162 try {
163 BufferedWriter myWriter = new BufferedWriter(
164 new OutputStreamWriter( myStream )
165 ) ;
166 for ( int Ii = 0 ; Ii < _dataList.size() ; Ii++ )
167 {
168 String str = (String)( _dataList.elementAt(Ii) )+ _CRLF_ ;
169 myWriter.write( str ) ;
170 // System.out.println("writing ..." + Ii ) ;
171 // brodcast a FileBufferTransfertEvent to any registered listener
172 if ( str != null )
173 broadcastBufferEvent(str.length()) ;
174 }
175 myWriter.flush() ;
176 } catch ( IOException e ) {
177 dataError( " Data thread Write Socket failed " + e.toString() ,
178 dataEvt
179 ) ;
180 }
181 }
182 else
183 dataError( " only ASCII file transfert is accepted " , dataEvt ) ;
184 }
185 // System.out.println("Notify for nb writes : " + _dataList.size() ) ;
186 // notify waiting thread about data availability
187 if ( dataEvt != null )
188 dataEvt.makeDataVisible() ;
189 }
190
191 private void writeFromFile( OutputStream myStream , FtpDataNotifier dataEvt )
192 {
193 try {
194 BufferedWriter myWriter = new BufferedWriter(
195 new OutputStreamWriter( myStream )
196 ) ;
197 String str = _localReader.readLine() ;
198 while ( str != null )
199 {
200 myWriter.write( str + _CRLF_ ) ;
201 broadcastBufferEvent(str.length()) ;
202 str = _localReader.readLine() ;
203 }
204 myWriter.flush() ;
205 } catch ( IOException e ) {
206 dataError( " Data thread Write Socket failed " + e.toString() ,
207 dataEvt
208 ) ;
209 }
210 // System.out.println("Notify for nb writes : " + _dataList.size() ) ;
211 // notify waiting thread about data availability
212 dataEvt.makeDataVisible() ;
213 }
214
215 // any Send of DATA to DATA SOCKET Shoud be done here
216 // ( override this method if you need a diferent container than Vector
217 public void writeData( OutputStream myStream , FtpDataNotifier dataEvt )
218 {
219 if ( _localReader == null )
220 writeFromBuffer( myStream , dataEvt ) ;
221 else
222 writeFromFile( myStream , dataEvt ) ;
223 }
224
225 /** unused */
226 public void readData( BufferedInputStream myStream ,
227 FtpDataNotifier dataEvt
228 )
229 {
230 }
231
232 /** unused */
233 public void writeData( BufferedOutputStream myWriter ,
234 FtpDataNotifier dataEvt
235 )
236
237 {
238 }
239
240 // Vector's accessor
241 public Vector get_dataList()
242 { return _dataList ; }
243
244 /**
245 the data byte buffer should be set before the writeDataMethod
246 is activated
247 */
248 public void set_dataList( Vector data )
249 { _dataList = data ; }
250
251 /**
252 if _dataList is not null return a concatenated String lines
253 */
254 public String toString()
255 {
256 StringBuffer returned = new StringBuffer() ;
257 if ( _dataList == null )
258 return null ;
259 Enumeration list = _dataList.elements() ;
260 while ( list.hasMoreElements() )
261 {
262 String data = (String) list.nextElement() ;
263 returned.append( data ) ;
264 returned.append(_CRLF_) ;
265 }
266 String appended = returned.toString() ;
267 return appended ;
268
269 }
270 }