]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/IOUtils.java
Move l10n-stuff to own package.
[gigi.git] / tests / org / cacert / gigi / testUtils / IOUtils.java
1 package org.cacert.gigi.testUtils;
2
3 import java.io.CharArrayWriter;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.net.HttpURLConnection;
8 import java.net.URLConnection;
9
10 public class IOUtils {
11
12     private IOUtils() {
13
14     }
15
16     public static String readURL(URLConnection in) {
17         try {
18             if ( !in.getContentType().equals("text/html; charset=UTF-8")) {
19                 if (in instanceof HttpURLConnection && ((HttpURLConnection) in).getResponseCode() != 200) {
20                     System.err.println(readURL(new InputStreamReader(((HttpURLConnection) in).getErrorStream(), "UTF-8")));
21                 }
22                 throw new Error("Unrecognized content-type: " + in.getContentType());
23             }
24             return readURL(new InputStreamReader(in.getInputStream(), "UTF-8"));
25         } catch (IOException e) {
26             throw new Error(e);
27         }
28
29     }
30
31     public static String readURL(Reader in) {
32         CharArrayWriter caw = new CharArrayWriter();
33         char[] buffer = new char[1024];
34         int len = 0;
35         try {
36             while ((len = in.read(buffer)) > 0) {
37                 caw.write(buffer, 0, len);
38             }
39             return new String(caw.toCharArray());
40         } catch (IOException e) {
41             throw new Error(e);
42         }
43
44     }
45 }