|
50 | 50 | import java.io.EOFException; |
51 | 51 | import java.io.IOException; |
52 | 52 | import java.io.InputStream; |
| 53 | +import java.io.InputStreamReader; |
53 | 54 | import java.io.OutputStream; |
| 55 | +import java.io.OutputStreamWriter; |
54 | 56 | import java.io.Reader; |
| 57 | +import java.io.Writer; |
55 | 58 | import java.io.UnsupportedEncodingException; |
56 | 59 | import java.util.ArrayList; |
57 | 60 | import java.util.Collection; |
@@ -107,6 +110,78 @@ public static void setIgnorCharsWhileEncoding(String s) { |
107 | 110 | ignoreCharsWhenEncoding = s; |
108 | 111 | } |
109 | 112 |
|
| 113 | + /** |
| 114 | + * Helper to get bytes from string with UTF-8 encoding |
| 115 | + * @param s the string |
| 116 | + * @return the bytes |
| 117 | + */ |
| 118 | + public static byte[] getBytes(String s) { |
| 119 | + try { |
| 120 | + return s.getBytes("UTF-8"); |
| 121 | + } catch(UnsupportedEncodingException e) { |
| 122 | + // never happens |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Helper to get string from bytes with UTF-8 encoding |
| 129 | + * @param b the bytes |
| 130 | + * @return the string |
| 131 | + */ |
| 132 | + public static String newString(byte[] b) { |
| 133 | + try { |
| 134 | + return new String(b, "UTF-8"); |
| 135 | + } catch(UnsupportedEncodingException e) { |
| 136 | + // never happens |
| 137 | + throw new RuntimeException(e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Helper to get string from bytes with UTF-8 encoding |
| 143 | + * @param b the bytes |
| 144 | + * @param offset the offset |
| 145 | + * @param length the length |
| 146 | + * @return the string |
| 147 | + */ |
| 148 | + public static String newString(byte[] b, int offset, int length) { |
| 149 | + try { |
| 150 | + return new String(b, offset, length, "UTF-8"); |
| 151 | + } catch(UnsupportedEncodingException e) { |
| 152 | + // never happens |
| 153 | + throw new RuntimeException(e); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Helper to get a reader from an input stream with UTF-8 encoding |
| 159 | + * @param in the input stream |
| 160 | + * @return the reader |
| 161 | + */ |
| 162 | + public static Reader getReader(InputStream in) { |
| 163 | + try { |
| 164 | + return new InputStreamReader(in, "UTF-8"); |
| 165 | + } catch(UnsupportedEncodingException e) { |
| 166 | + // never happens |
| 167 | + throw new RuntimeException(e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Helper to get a writer from an output stream with UTF-8 encoding |
| 173 | + * @param out the output stream |
| 174 | + * @return the writer |
| 175 | + */ |
| 176 | + public static Writer getWriter(OutputStream out) { |
| 177 | + try { |
| 178 | + return new OutputStreamWriter(out, "UTF-8"); |
| 179 | + } catch(UnsupportedEncodingException e) { |
| 180 | + // never happens |
| 181 | + throw new RuntimeException(e); |
| 182 | + } |
| 183 | + } |
| 184 | + |
110 | 185 | /** |
111 | 186 | * Copy the input stream into the output stream, closes both streams when finishing or in |
112 | 187 | * a case of an exception |
|
0 commit comments