Fix minor bug in strength checker and add unit test.
[gigi.git] / tests / org / cacert / gigi / IOUtils.java
1 package org.cacert.gigi;
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.URLConnection;
8
9 public class IOUtils {
10         private IOUtils() {
11
12         }
13         public static String readURL(URLConnection in) {
14                 try {
15                         if (!in.getContentType().equals("text/html; charset=UTF-8")) {
16                                 throw new Error("Unrecognized content-type: "
17                                                 + in.getContentType());
18                         }
19                         return readURL(new InputStreamReader(in.getInputStream(), "UTF-8"));
20                 } catch (IOException e) {
21                         throw new Error(e);
22                 }
23
24         }
25         public static String readURL(Reader in) {
26                 CharArrayWriter caw = new CharArrayWriter();
27                 char[] buffer = new char[1024];
28                 int len = 0;
29                 try {
30                         while ((len = in.read(buffer)) > 0) {
31                                 caw.write(buffer, 0, len);
32                         }
33                         return new String(caw.toCharArray());
34                 } catch (IOException e) {
35                         throw new Error(e);
36                 }
37
38         }
39 }