String makeString(int[] codes, boolean debug) throws UnsupportedEncodingException { byte[] b = new byte[codes.length]; for (int i=0; i<codes.length; i++) b[i] = (byte) codes[i]; if (debug) System.out.println(" orgBytes= "+showBytes(b)); String s = new String(b, "UTF-8"); if (debug) System.out.println("convBytes= "+showString( s)); return s; }
public static void main(String args[]) throws IOException { //String helloGreek = new TestUnicode("dumm").makeString(helloGreekCode, true); //testCharsets(); write("hello", null); write("hello", "UTF-8"); write("hello", "EUC-JP"); //String filename = "C:/data/unicode/UTF-8-demo.html"; //String filename = "C:/data/unicode/russian.txt"; //testRAF(filename); //testUnicodeRead(filename); //testRead(filename); }
String helloGreek = makeString(helloGreekCode, true); String s2 ="";// = Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("char values= "+showString(helloGreek)); System.out.println(" UTF-8= "+showBytes(helloGreek.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(helloGreek.getBytes("UTF-16"))); System.out.println(" default= "+showBytes(helloGreek.getBytes())); System.out.println("norm values= "+showString(s2)); System.out.println(" UTF-8= "+showBytes(s2.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(s2.getBytes("UTF-16"))); System.out.println(" default= "+showBytes(s2.getBytes())); write(helloGreek, null); write(helloGreek, "UTF-8");
public void makeNetCDF() throws IOException, InvalidRangeException { String helloGreek = makeString(helloGreekCode, true); helloGreek = ""; //Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("normalized= "+showString(helloGreek)); String filename = "C:/data/unicode/helloNorm.nc"; NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(filename, true); ucar.nc2.Dimension dim = ncfile.addDimension(helloGreek, 20); ncfile.addVariable(helloGreek, DataType.CHAR, helloGreek); ncfile.addVariableAttribute(helloGreek, "units", helloGreek); ncfile.create(); ArrayChar.D1 data = new ArrayChar.D1(dim.getLength()); data.setString(helloGreek); ncfile.write(helloGreek, data); ncfile.close(); NetcdfFile nc = NetcdfFile.open(filename); Variable v = ncfile.findVariable(helloGreek); assert v != null; assert v.getShortName().equals(helloGreek); Attribute att = v.findAttribute("units"); assert att != null; assert att.isString(); assert(helloGreek.equals(att.getStringValue())); nc.close(); }
static void testUnicodeRead(String filename) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename), "UTF-8")); int count = 0; String line; while (null != (line = in.readLine())) { System.out.println("ULine "+count++); System.out.println(line); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); } in.close(); }
static void testRead(String filename) throws IOException { DataInputStream in = new DataInputStream( new BufferedInputStream(new FileInputStream(filename))); int count = 0; String line; while (null != (line = in.readLine())) { System.out.println("Line "+count++); System.out.println(line); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); } in.close(); }
static void testRAF(String filename) throws IOException { ucar.unidata.io.RandomAccessFile raf = new ucar.unidata.io.RandomAccessFile(filename, "r"); int nelems = 37; byte[] b = new byte[nelems]; raf.read(b); String line = new String(b, "UTF-8"); // all strings are considered to be UTF-8 unicode. System.out.println(line); System.out.println(showBytes(b)); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); }