1 package org.simantics.databoard.tests;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.nio.charset.Charset;
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.binding.StringBinding;
14 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
16 public class StringUTFTest {
18 public static void main(String[] args) throws Exception {
20 StringBinding b = Bindings.STRING;
22 String[] strs = new String[c];
23 for (int i=0; i<c; i++) strs[i] = (String) b.createRandom(i);
27 ByteArrayOutputStream baos = new ByteArrayOutputStream( 10*1024*1024 );
28 DataOutputStream dos = new DataOutputStream( baos );
30 long x = System.nanoTime();
31 for (int i=0; i<c; i++) {
32 dos.writeUTF(strs[i]);
34 long y = System.nanoTime() - x;
35 System.out.println("DataOutputStream.writeUTF: "+y);
37 byte[] data = baos.toByteArray();
38 DataInputStream dis = new DataInputStream( new ByteArrayInputStream( data ) );
39 x = System.nanoTime();
40 for (int i=0; i<c; i++) {
43 y = System.nanoTime() - x;
44 System.out.println("DataOutputStream.readUTF : "+y);
48 Charset cs = Charset.forName("UTF8");
49 ByteArrayOutputStream baos = new ByteArrayOutputStream( 10*1024*1024 );
50 DataOutputStream dos = new DataOutputStream( baos );
52 long x = System.nanoTime();
53 for (int i=0; i<c; i++) {
54 byte[] data = strs[i].getBytes(cs);
55 dos.writeInt(data.length);
58 long y = System.nanoTime() - x;
59 System.out.println("String#getBytes(cs) : "+y);
62 byte[] strData = new byte[1024];
63 byte[] data = baos.toByteArray();
64 DataInputStream dis = new DataInputStream( new ByteArrayInputStream( data ) );
65 x = System.nanoTime();
66 for (int i=0; i<c; i++) {
67 int len = dis.readInt();
68 dis.read(strData, 0, len);
69 new String(strData, cs);
71 y = System.nanoTime() - x;
72 System.out.println("new String(byte[], cs) : "+y);