1 19 package org.openide.filesystems.data; 20 21 import java.util.ArrayList ; 22 import java.io.*; 23 24 27 public class SerialData extends Object implements Serializable { 28 29 private static String serialData; 30 31 private int data1; 32 private String data2; 33 private Object data3; 34 private ArrayList data4; 35 36 37 public SerialData() { 38 data1 = 0; 39 data2 = "data2"; 40 data3 = new Integer (5); 41 data4 = new ArrayList (); 42 data4.add(data3); 43 } 44 45 public static String getSerialDataString() throws Exception { 46 if (serialData == null) { 47 serialData = createSerialDataString(); 48 } 49 50 return serialData; 51 } 52 53 private static String createSerialDataString() throws Exception { 54 ByteArrayOutputStream barros = new ByteArrayOutputStream(); 55 ObjectOutputStream obos = new ObjectOutputStream(barros); 56 57 obos.writeObject(new SerialData()); 58 obos.close(); 59 60 return bytes2String(barros.toByteArray()); 61 } 62 63 66 77 78 private static String bytes2String (byte[] bytes) { 79 StringBuffer buffer = new StringBuffer (2 * bytes.length); 80 81 for (int i = 0; i < bytes.length; i++) { 82 addByte(bytes[i], buffer); 83 } 84 85 return buffer.toString(); 86 } 87 88 private static void addByte(int b, StringBuffer buffer) { 89 if (b < 0) { 90 b += 256; 91 } 92 93 int rest = b % 16; 94 b = b / 16; 95 buffer.append(toChar(b)); 96 buffer.append(toChar(rest)); 97 } 98 99 private static char toChar(int b) { 100 if (b > 9) { 101 return (char) ('a' + b - 10); 102 } else { 103 return (char) ('0' + b); 104 } 105 } 106 } 107 | Popular Tags |