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.net;
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.net.HttpURLConnection;
27  import java.net.URL;
28  
29  /***
30   * This class provides a convenience wrapper for the 
31   * {@link java.net.HttpURLConnection} class.
32   */
33  public class HttpConnection {
34  
35      //--- Data Field(s) ---
36  
37      private BufferedReader in = null;
38      private HttpURLConnection connection = null;
39  
40      //--- Constructor(s) ---
41  	
42      /***
43       * Constructs a http connection. No action is taken.
44       */ 
45      public HttpConnection()
46      {
47      }
48  
49      //--- Method(s) ---
50  
51      /***
52       * Establishes a connection to <code>url</code>.
53       *
54       * @param url the url to connect to
55       * @exception IOException thrown if the connection fails
56       */
57      public void connect(URL url) throws IOException
58      {
59  		connection = (HttpURLConnection)url.openConnection();
60  		if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
61  			in = new BufferedReader
62  				(new InputStreamReader(connection.getInputStream()));
63  	    
64  		} 
65  		else {
66  			connection.disconnect();
67  			throw new IOException(connection.getResponseMessage());
68  		}
69      }
70  
71      /***
72       * Establishes a connection to <code>location</code>.
73       *
74       * @param location the url to connect to
75       * @exception IOException thrown if the connection fails
76       */
77      public void connect(String location) throws IOException
78      {
79  		connect(new URL(location));
80      }
81  
82      /***
83       * Closes the connection.
84       */
85      public void close()
86      {
87  		connection.disconnect();
88      }
89  
90      /***
91       * Returns the associated input stream. The connection needs to be
92       * established prior to invoking this method.
93       * 
94       * @see #connect(URL)
95       */
96      public InputStream getInputStream() throws IOException
97      {
98  		return connection.getInputStream();
99      }
100 
101     /***
102      * Reads and returns the next line from the input stream. The connection
103      * is automatically closed if the end of the stream is reached.
104      *
105      * @return null, if the end of the stream is reached or an exception
106      *         occurs
107      */
108     public String nextLine()
109     {
110 		if (in == null) {
111 			return null;
112 		}
113 
114 		String line = null;
115         try {
116             line = in.readLine();
117         } 
118 		catch(IOException e) {
119         }
120 
121 		if (line == null) {
122 			connection.disconnect();
123 		}
124 
125 		return line;
126     }
127 
128 	//      public static void save(InputStream in, String filename) throws IOException
129 	//      {
130 	//  	byte[] b = new byte[500];
131 
132 	//  	OutputStream out = new FileOutputStream(filename, false);
133 
134 	//  	int c = 0;
135 	//  	while (c != -1) {
136 	//  	    out.write(b, 0, c);
137 	//  	    c = in.read(b);
138 	//  	}
139 
140 	//  	out.close();
141 	//      }
142 
143 }