KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > data > SerialData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.filesystems.data;
20
21 import java.util.ArrayList JavaDoc;
22 import java.io.*;
23
24 /**
25  * Serves for generating
26 */

27 public class SerialData extends Object JavaDoc implements Serializable {
28
29     private static String JavaDoc serialData;
30
31     private int data1;
32     private String JavaDoc data2;
33     private Object JavaDoc data3;
34     private ArrayList JavaDoc data4;
35
36     /** Creates new SerialData */
37     public SerialData() {
38         data1 = 0;
39         data2 = "data2";
40         data3 = new Integer JavaDoc(5);
41         data4 = new ArrayList JavaDoc();
42         data4.add(data3);
43     }
44
45     public static String JavaDoc getSerialDataString() throws Exception JavaDoc {
46         if (serialData == null) {
47             serialData = createSerialDataString();
48         }
49         
50         return serialData;
51     }
52     
53     private static String JavaDoc createSerialDataString() throws Exception JavaDoc {
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     /**
64     * @param args the command line arguments
65     */

66     /*
67     public static void main(String args[]) throws Exception {
68         ByteArrayOutputStream barros = new ByteArrayOutputStream();
69         ObjectOutputStream obos = new ObjectOutputStream(barros);
70         
71         obos.writeObject(new SerialData());
72         obos.close();
73         
74         byte[] bytes = barros.toByteArray();
75         System.out.println(bytes2String(bytes));
76     }*/

77     
78     private static String JavaDoc bytes2String (byte[] bytes) {
79         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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 JavaDoc 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