001
002
003 import java.io.IOException;
004 import java.io.OutputStream;
005 import java.io.InputStream;
006 import java.io.ByteArrayOutputStream;
007 import java.io.ByteArrayInputStream;
008 import java.io.ObjectOutputStream;
009 import java.io.ObjectInputStream;
010 import java.io.FileOutputStream;
011 import java.io.FileInputStream;
012 import java.io.FileNotFoundException;
013
014 import java.nio.ByteBuffer;
015 import java.nio.channels.FileChannel;
016
017
018 /**
019 * Versuch mit Object Streams und nio.Buffer
020 */
021
022 public class ObjectBufferTest {
023
024 final static int SIZE = 4*1024;
025
026 public static void main( String[] args ) {
027 testOutput();
028 testInput();
029 }
030
031
032 /**
033 * Output 3 objects to a file using a buffer.
034 */
035 private static void testOutput() {
036 ByteBuffer bbd = ByteBuffer.allocate( SIZE );
037 bbd.clear();
038 ObjectBuffer ob = new ObjectBuffer( bbd );
039 System.out.println("ob = " + ob );
040
041 try {
042 Object tmp = new java.util.HashMap(44);
043 System.out.println("tmp = " + tmp);
044 ob.put( tmp );
045
046 tmp = new java.util.Date();
047 System.out.println("tmp = " + tmp);
048 ob.put( tmp );
049
050 tmp = new java.math.BigInteger("393993");
051 System.out.println("tmp = " + tmp);
052 ob.put( tmp );
053 } catch(IOException e) {
054 e.printStackTrace();
055 return;
056 }
057 System.out.println("ob = " + ob );
058 bbd.flip();
059
060 String filename = "File2Name";
061 FileOutputStream fos = null;
062 try {
063 fos = new FileOutputStream( filename /*, true*/ );
064 } catch(IOException e) {
065 e.printStackTrace();
066 return;
067 }
068 FileChannel fc = fos.getChannel();
069 try {
070 fc.write( bbd );
071 fc.force(true);
072 fc.close();
073 } catch(IOException e) {
074 e.printStackTrace();
075 return;
076 }
077 //System.out.println("bbd = " + bbd );
078 bbd.rewind();
079 //System.out.println("bbd.size = " + bbd.limit() );
080 System.out.println("+++------------------");
081 }
082
083
084
085 /**
086 * Input 3 objects from a file using a buffer.
087 */
088 private static void testInput( ) {
089 String filename = "File2Name";
090 FileInputStream fis = null;
091 try {
092 fis = new FileInputStream( filename );
093 } catch(IOException e) {
094 e.printStackTrace();
095 return;
096 }
097 FileChannel fc = fis.getChannel();
098 ByteBuffer bbw = ByteBuffer.allocateDirect( SIZE );
099 bbw.clear();
100 int count = 0;
101 try {
102 count = fc.read( bbw );
103 //System.out.println("count = " + count);
104 if ( count >= SIZE ) { // should not happen
105 System.out.println("need a loop");
106 }
107 fc.close();
108 } catch(IOException e) {
109 e.printStackTrace();
110 return;
111 }
112 bbw.flip();
113
114 ObjectBuffer ob = new ObjectBuffer( bbw );
115 System.out.println("ob = " + ob );
116 Object tmp = null;
117 try {
118 tmp = ob.get();
119 System.out.println("tmp = " + tmp);
120
121 tmp = ob.get();
122 System.out.println("tmp = " + tmp);
123
124 tmp = ob.get();
125 System.out.println("tmp = " + tmp);
126 } catch(IOException e) {
127 e.printStackTrace();
128 return;
129 } catch(ClassNotFoundException e) {
130 e.printStackTrace();
131 return;
132 }
133 System.out.println("ob = " + ob );
134
135 System.out.println("***------------------");
136 }
137
138 }