| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NativeAsciiReader |
|
| 11.666666666666666;11.667 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | ||
| 23 | package org.webmacro.util; | |
| 24 | ||
| 25 | import java.io.BufferedReader; | |
| 26 | import java.io.IOException; | |
| 27 | import java.io.Reader; | |
| 28 | ||
| 29 | /** | |
| 30 | * Reads a file that has been encoded with Java's unicode escape syntax, | |
| 31 | * typically converted with native2ascii. | |
| 32 | * | |
| 33 | * All character sequences like \\uxxxx will be converted to a single unicode | |
| 34 | * character. Other '\' escaped characters will be passed unchanged. An | |
| 35 | * error will be thrown if the \\u is not followed by 4 hexadecimal characters | |
| 36 | * | |
| 37 | * | |
| 38 | */ | |
| 39 | public class NativeAsciiReader extends BufferedReader | |
| 40 | { | |
| 41 | ||
| 42 | /* | |
| 43 | The constructors simply echo those in the super-class | |
| 44 | */ | |
| 45 | 0 | private static int defaultCharBufferSize = 8192; |
| 46 | public static final String RCS = "$Id: org.webmacro.util.NativeAsciiReader.html,v 1.1 2010/03/04 23:00:06 timp Exp $"; | |
| 47 | ||
| 48 | public NativeAsciiReader (Reader in, int sz) | |
| 49 | { | |
| 50 | 0 | super(in, sz); |
| 51 | 0 | } |
| 52 | ||
| 53 | public NativeAsciiReader (Reader in) | |
| 54 | { | |
| 55 | 0 | this(in, defaultCharBufferSize); |
| 56 | 0 | } |
| 57 | ||
| 58 | /** | |
| 59 | * Read a sequence of characters into the given buffer. Since we're | |
| 60 | * extending BufferedReader, it is efficient enough to read one character | |
| 61 | * at a time | |
| 62 | */ | |
| 63 | public int read (char cbuf[], int off, int len) throws IOException | |
| 64 | { | |
| 65 | 0 | int i = 0; |
| 66 | 0 | while (i < len) |
| 67 | { | |
| 68 | 0 | int c = read(); |
| 69 | 0 | if (i == 0 && c == -1) return -1; |
| 70 | 0 | if (c == -1) return i; |
| 71 | 0 | if (c == '\\') |
| 72 | { | |
| 73 | 0 | mark(1); |
| 74 | 0 | if (read() != 'u') |
| 75 | { | |
| 76 | 0 | reset(); |
| 77 | } | |
| 78 | 0 | int value = 0; |
| 79 | int a; | |
| 80 | ||
| 81 | 0 | for (int j = 0; j < 4; j++) |
| 82 | { | |
| 83 | 0 | a = read(); |
| 84 | 0 | switch (a) |
| 85 | { | |
| 86 | case '0': | |
| 87 | case '1': | |
| 88 | case '2': | |
| 89 | case '3': | |
| 90 | case '4': | |
| 91 | case '5': | |
| 92 | case '6': | |
| 93 | case '7': | |
| 94 | case '8': | |
| 95 | case '9': | |
| 96 | 0 | value = (value << 4) + a - '0'; |
| 97 | 0 | break; |
| 98 | case 'a': | |
| 99 | case 'b': | |
| 100 | case 'c': | |
| 101 | case 'd': | |
| 102 | case 'e': | |
| 103 | case 'f': | |
| 104 | 0 | value = (value << 4) + 10 + a - 'a'; |
| 105 | 0 | break; |
| 106 | case 'A': | |
| 107 | case 'B': | |
| 108 | case 'C': | |
| 109 | case 'D': | |
| 110 | case 'E': | |
| 111 | case 'F': | |
| 112 | 0 | value = (value << 4) + 10 + a - 'A'; |
| 113 | 0 | break; |
| 114 | default: | |
| 115 | 0 | throw new IllegalArgumentException( |
| 116 | "Malformed \\uxxxx encoding."); | |
| 117 | } | |
| 118 | } | |
| 119 | 0 | c = (char) value; |
| 120 | } | |
| 121 | 0 | cbuf[i + off] = (char) c; |
| 122 | 0 | i++; |
| 123 | 0 | } |
| 124 | 0 | return i; |
| 125 | } | |
| 126 | ||
| 127 | } |