| 13 | import avian.Utf8; |
| 14 | |
| 15 | public class InputStreamReader extends Reader { |
| 16 | private static final int MultibytePadding = 4; |
| 17 | |
| 18 | private final InputStream in; |
| 19 | |
| 20 | public InputStreamReader(InputStream in) { |
| 21 | this.in = in; |
| 22 | } |
| 23 | |
| 24 | public InputStreamReader(InputStream in, String encoding) |
| 25 | throws UnsupportedEncodingException |
| 26 | { |
| 27 | this(in); |
| 28 | |
| 29 | if (! encoding.equals("UTF-8")) { |
| 30 | throw new UnsupportedEncodingException(encoding); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public int read(char[] b, int offset, int length) throws IOException { |
| 35 | if (length == 0) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | byte[] buffer = new byte[length + MultibytePadding]; |
| 40 | int bufferLength = length; |
| 41 | int bufferOffset = 0; |
| 42 | while (true) { |
| 43 | int c = in.read(buffer, bufferOffset, bufferLength); |
| 44 | |
| 45 | if (c <= 0) { |
| 46 | if (bufferOffset > 0) { |
| 47 | // if we've reached the end of the stream while trying to |
| 48 | // read a multibyte character, we still need to return any |
| 49 | // competely-decoded characters, plus \ufffd to indicate an |
| 50 | // unknown character |
| 51 | c = 1; |
| 52 | while (bufferOffset > 0) { |
| 53 | char[] buffer16 = Utf8.decode16(buffer, 0, bufferOffset); |
| 54 | |
| 55 | if (buffer16 != null) { |
| 56 | System.arraycopy(buffer16, 0, b, offset, buffer16.length); |
| 57 | |
| 58 | c = buffer16.length + 1; |
| 59 | break; |
| 60 | } else { |
| 61 | -- bufferOffset; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | b[offset + c - 1] = '\ufffd'; |
| 66 | } |
| 67 | |
| 68 | return c; |
| 69 | } |
| 70 | |
| 71 | bufferOffset += c; |
| 72 |
nothing calls this directly
no outgoing calls
no test coverage detected