View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
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 org.xnap.plugin.opennap.net;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.Socket;
25  
26  import org.xnap.XNap;
27  import org.xnap.util.QuotedStringTokenizer;
28  
29  public class UploadSocket extends IncomingSocket {
30  
31      //--- Data Field(s) ---
32      
33      public String requestFilename;
34      public String nick;
35      public long offset;
36  
37      //--- Constructor(s) ---
38  
39      /***
40       * This is a bit ugly since we have to read in the file offset too and
41       * pass it on to the actual upload.
42       */
43      public UploadSocket(Socket socket, InputStream in) throws IOException
44      {
45  		super(socket, in);
46  
47  		byte data[] = new byte[2048];
48  		int j = in.read(data);
49  		if (j < 0) {
50  			throw new IOException(XNap.tr("invalid request"));
51  		}
52  		String response = new String(data, 0, j);
53  	    
54  		QuotedStringTokenizer t = new QuotedStringTokenizer(response);
55  
56  		if (t.countTokens() < 3) {
57  			throw new IOException(XNap.tr("invalid request: {0}", response));
58  		}
59  
60  		nick = t.nextToken();
61  		requestFilename = t.nextToken();
62  
63  		try {
64  			offset = Long.parseLong(t.nextToken());
65  		}
66  		catch (NumberFormatException e) {
67  			throw new IOException(XNap.tr("invalid request"));
68  		}		
69      }
70  
71      //--- Method(s) ---
72  
73  }
74  
75