KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > HttpTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.common;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26
27 /**
28  * This class supplies some methods of usefullness
29  * for the management of HTTP connection.
30  *
31  * @version $Id: HttpTools.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
32  * @author Stefano Nichele
33  */

34
35 public class HttpTools {
36
37
38     // -----------------------------------------------------------Public methods
39
/**
40      * Download file from the given location.
41      *
42      * @param fileUrl location from which download the file.
43      * @return the content of the file downloaded.
44      * @throws IOException if an I/O error occurs.
45      */

46     public static byte[] downloadPackage(String JavaDoc fileUrl)
47     throws IOException JavaDoc {
48         byte[] content = null;
49
50         int size = -1;
51
52         URL JavaDoc url = new URL JavaDoc(fileUrl);
53
54         URLConnection JavaDoc urlConn = url.openConnection();
55         url.openStream();
56         size = urlConn.getContentLength();
57         content = new byte[size];
58
59         InputStream JavaDoc iStream = url.openStream();
60         int read = -1;
61         int offset = 0;
62         int toRead = size;
63         while ( (read = iStream.read(content, offset,toRead)) != -1) {
64             toRead = toRead - read;
65             offset = offset + read;
66         }
67         iStream.close();
68         return content;
69
70     }
71
72     /**
73      * Returns true if the given url is a valid http(s) url.
74      *
75      * @param url - NOT NULL
76      *
77      * @return true if <i>url</i>is a valid http(s) url, false otherwise
78      */

79     public static boolean isHTTPURL(String JavaDoc url) {
80         if (!(url.startsWith("http://") || url.startsWith("https://"))) {
81             return false;
82         }
83         
84         try {
85             new URL JavaDoc(url);
86             return true;
87         } catch (MalformedURLException JavaDoc e) {}
88         
89         return false;
90     }
91
92
93 }
Popular Tags