1 21 22 package org.apache.derbyTesting.functionTests.util.streams; 23 24 import java.io.UnsupportedEncodingException ; 25 26 41 public class ByteAlphabet { 42 43 44 private final String name; 45 46 private final String encoding; 47 48 private final byte[] bytes; 49 50 private final int charCount; 51 52 private final int byteCount; 53 54 private int boff = 0; 55 56 60 public static ByteAlphabet modernLatinLowercase() { 61 return new ByteAlphabet("Modern latin lowercase, US-ASCII", 62 CharAlphabet.MODERNLATINLOWER, 63 "US-ASCII"); 64 } 65 66 70 public static ByteAlphabet norwegianLowercase() { 71 return new ByteAlphabet("Norwegian/Danish lowercase, ISO-8859-1", 72 CharAlphabet.NO_DK_LOWER, 73 "ISO-8859-1"); 74 } 75 76 80 public static ByteAlphabet tamilUTF8() { 81 return new ByteAlphabet("Tamil, UTF-8", 82 CharAlphabet.TAMIL, 83 "UTF8"); 84 } 85 86 90 public static ByteAlphabet tamilUTF16BE() { 91 return new ByteAlphabet("Tamil, UTF-16BE", 92 CharAlphabet.TAMIL, 93 "UTF-16BE"); 94 } 95 96 100 public static ByteAlphabet cjkSubsetUTF8() { 101 return new ByteAlphabet("CJK subset, UTF-8", 102 CharAlphabet.CJKSUBSET, 103 "UTF8"); 104 } 105 106 110 public static ByteAlphabet cjkSubsetUTF16BE() { 111 return new ByteAlphabet("CJK subset, UTF-16BE", 112 CharAlphabet.CJKSUBSET, 113 "UTF-16BE"); 114 } 115 116 124 private ByteAlphabet(String name, char[] chars, String encoding) { 125 this.name = name; 126 this.encoding = encoding; 127 this.charCount = chars.length; 128 String tmpStr = new String (chars); 129 byte[] tmpBytes; 130 int tmpByteCount; 131 try { 132 tmpBytes = tmpStr.getBytes(encoding); 133 tmpByteCount = tmpBytes.length; 134 } catch (UnsupportedEncodingException uee) { 135 tmpBytes = new byte[] {0}; 137 tmpByteCount = 1; 138 } 139 this.bytes = tmpBytes; 140 this.byteCount = tmpByteCount; 141 } 142 143 146 public String getName() { 147 return this.name; 148 } 149 150 153 public String getEncoding() { 154 return this.encoding; 155 } 156 157 160 public int charCount() { 161 return charCount; 162 } 163 164 172 public int byteCount() { 173 return byteCount; 174 } 175 176 179 public byte nextByte() { 180 if (boff >= byteCount) { 181 boff = 0; 182 } 183 return bytes[boff++]; 184 } 185 186 190 public void reset() { 191 boff = 0; 192 } 193 194 204 public int nextByteToRead(int bytesRead) { 205 boff = (boff + (bytesRead % byteCount)) % byteCount; 206 return boff; 207 } 208 } | Popular Tags |