View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.file;
21  
22  import org.waarp.common.command.exception.CommandAbstractException;
23  import org.waarp.common.command.exception.Reply530Exception;
24  import org.waarp.common.exception.FileEndOfTransferException;
25  import org.waarp.common.exception.FileTransferException;
26  
27  import java.io.IOException;
28  
29  /**
30   * Interface for File support
31   */
32  public interface FileInterface {
33    /**
34     * Set empty this FtpFile, mark it unReady.
35     *
36     * @throws CommandAbstractException
37     */
38    void clear() throws CommandAbstractException;
39  
40    /**
41     * Check if the authentication is correct
42     *
43     * @throws Reply530Exception
44     */
45    void checkIdentify() throws Reply530Exception;
46  
47    /**
48     * @return the FtpSession
49     */
50    SessionInterface getSession();
51  
52    // **************** Directory part **************************
53  
54    /**
55     * @return the FtpDir associated at creation with this file
56     */
57    DirInterface getDir();
58  
59    /**
60     * Is the current FileInterface a directory and exists
61     *
62     * @return True if it is a directory and it exists
63     *
64     * @throws CommandAbstractException
65     */
66    boolean isDirectory() throws CommandAbstractException;
67  
68    /**
69     * Is the current FileInterface a file and exists
70     *
71     * @return True if it is a file and it exists
72     *
73     * @throws CommandAbstractException
74     */
75    boolean isFile() throws CommandAbstractException;
76  
77    // **************** Unique FileInterface part **************************
78  
79    /**
80     * @return the path of the current FileInterface (without mount point if
81     *     any)
82     *
83     * @throws CommandAbstractException
84     */
85    String getFile() throws CommandAbstractException;
86  
87    /**
88     * Close the current FileInterface
89     *
90     * @return True if correctly closed
91     *
92     * @throws CommandAbstractException
93     */
94    boolean closeFile() throws CommandAbstractException;
95  
96    /**
97     * @return the length of the current FileInterface
98     *
99     * @throws CommandAbstractException
100    */
101   long length() throws CommandAbstractException;
102 
103   /**
104    * @return True if the current FileInterface is in Writing process
105    *
106    * @throws CommandAbstractException
107    */
108   boolean isInWriting() throws CommandAbstractException;
109 
110   /**
111    * @return True if the current FileInterface is in Reading process
112    *
113    * @throws CommandAbstractException
114    */
115   boolean isInReading() throws CommandAbstractException;
116 
117   /**
118    * @return True if the current FileInterface is ready for reading
119    *
120    * @throws CommandAbstractException
121    */
122   boolean canRead() throws CommandAbstractException;
123 
124   /**
125    * @return True if the current FileInterface is ready for writing
126    *
127    * @throws CommandAbstractException
128    */
129   boolean canWrite() throws CommandAbstractException;
130 
131   /**
132    * @return True if the current FileInterface exists
133    *
134    * @throws CommandAbstractException
135    */
136   boolean exists() throws CommandAbstractException;
137 
138   /**
139    * Try to abort the current transfer if any
140    *
141    * @return True if everything is ok
142    *
143    * @throws CommandAbstractException
144    */
145   boolean abortFile() throws CommandAbstractException;
146 
147   /**
148    * Ask to store the current FileInterface. This command returns quickly
149    * since
150    * it does not store really. It
151    * prepares the object.
152    *
153    * @return True if everything is ready
154    *
155    * @throws CommandAbstractException
156    */
157   boolean store() throws CommandAbstractException;
158 
159   /**
160    * Ask to retrieve the current FileInterface. This command returns quickly
161    * since it does not retrieve really.
162    * It prepares the object.
163    *
164    * @return True if everything is ready
165    *
166    * @throws CommandAbstractException
167    */
168   boolean retrieve() throws CommandAbstractException;
169 
170   /**
171    * Rename the current FileInterface into a new filename from argument
172    *
173    * @param path the new filename (path could be relative or absolute
174    *     -
175    *     without mount point)
176    *
177    * @return True if the operation is done successfully
178    *
179    * @throws CommandAbstractException
180    */
181   boolean renameTo(String path) throws CommandAbstractException;
182 
183   /**
184    * Restart from a Marker for the current FileInterface if any. This function
185    * is to be called at the beginning
186    * of every transfer so in store and retrieve method.
187    *
188    * @param restart
189    *
190    * @return True if the Marker is OK
191    *
192    * @throws CommandAbstractException
193    */
194   boolean restartMarker(Restart restart) throws CommandAbstractException;
195 
196   /**
197    * Create a restart from context for the current FileInterface
198    *
199    * @return the dataBlock to send to the client
200    *
201    * @throws CommandAbstractException
202    */
203   DataBlock getMarker() throws CommandAbstractException;
204 
205   /**
206    * Delete the current FileInterface.
207    *
208    * @return True if OK, else False if not (or if the file never exists).
209    *
210    * @throws CommandAbstractException
211    */
212   boolean delete() throws CommandAbstractException;
213 
214   /**
215    * Change the position in the file.
216    *
217    * @param position the position to set
218    *
219    * @throws IOException
220    */
221   void setPosition(long position) throws IOException;
222 
223   /**
224    * Function called by the DataNetworkHandler when it receives one DataBlock
225    * (Store like command)
226    *
227    * @param dataBlock
228    *
229    * @throws FileTransferException
230    * @throws FileEndOfTransferException
231    */
232   void writeDataBlock(DataBlock dataBlock) throws FileTransferException;
233 
234   /**
235    * Read a new block for FileInterface
236    *
237    * @return dataBlock
238    *
239    * @throws FileEndOfTransferException
240    * @throws FileTransferException
241    */
242   DataBlock readDataBlock()
243       throws FileEndOfTransferException, FileTransferException;
244 
245   /**
246    * Read a new block for FileInterface
247    *
248    * @param buffer use the given buffer to read
249    *
250    * @return dataBlock
251    *
252    * @throws FileEndOfTransferException
253    * @throws FileTransferException
254    */
255   DataBlock readDataBlock(byte[] buffer)
256       throws FileEndOfTransferException, FileTransferException;
257 }