View Javadoc

1   package org.csc.phynixx.common.io;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 Christoph Schmidt-Casdorff
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import org.apache.commons.io.IOUtils;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  import java.io.ObjectOutputStream;
30  
31  /**
32   * Created by christoph on 12.01.14.
33   */
34  public class LogRecordWriter {
35  
36  
37      private final ByteArrayOutputStream byteInput;
38  
39      private final DataOutputStream io;
40  
41      public LogRecordWriter() {
42          this.byteInput = new ByteArrayOutputStream();
43          this.io = new DataOutputStream(this.byteInput);
44      }
45  
46  
47      public byte[] toByteArray() throws IOException {
48          this.io.flush();
49          return byteInput.toByteArray();
50      }
51  
52      public LogRecordWriter writeChar(int v) throws IOException {
53          io.writeChar(v);
54          return this;
55      }
56  
57      public LogRecordWriter writeShort(int v) throws IOException {
58          io.writeShort(v);
59          return this;
60      }
61  
62      public LogRecordWriter writeInt(int v) throws IOException {
63          io.writeInt(v);
64          return this;
65      }
66  
67      public LogRecordWriter writeLong(long v) throws IOException {
68          io.writeLong(v);
69          return this;
70      }
71  
72      public LogRecordWriter writeFloat(float v) throws IOException {
73          io.writeFloat(v);
74          return this;
75      }
76  
77      public LogRecordWriter writeDouble(double v) throws IOException {
78          io.writeDouble(v);
79          return this;
80      }
81  
82      public LogRecordWriter writeUTF(String str) throws IOException {
83          io.writeUTF(str);
84          return this;
85      }
86  
87      public LogRecordWriter writeByte(byte b) throws IOException {
88          io.writeByte(b);
89          return this;
90      }
91  
92      public LogRecordWriter writeBoolean(boolean v) throws IOException {
93          io.writeBoolean(v);
94          return this;
95      }
96  
97      /**
98       * write the object's class name to check the consistency if the restroe fails.
99       *
100      * A null object is accepted
101      *
102      * @param object serializable object. it has to fullfill the requirements of {@link java.io.ObjectOutputStream#writeObject(Object)} .
103      * @return returns the fluent API
104      * @throws IOException
105      */
106     public LogRecordWriter writeObject(Object object) throws IOException {
107         if( object==null) {
108             return this.writeNullObject();
109         }
110         ObjectOutputStream out=null;
111         try {
112             ByteArrayOutputStream byteOutput= new ByteArrayOutputStream();
113             out = new ObjectOutputStream(byteOutput);
114             out.writeObject(object);
115             out.flush();
116             byte[] serBytes=byteOutput.toByteArray();
117             io.writeInt(serBytes.length);
118             io.write(serBytes);
119         } finally {
120             if(out!=null) {
121                 IOUtils.closeQuietly(out);
122             }
123         }
124         return this;
125     }
126 
127     /**
128      * A null object is accepted
129      *
130      *  @return returns the fluent API
131      * @throws IOException
132      */
133     private LogRecordWriter writeNullObject() throws IOException {
134             io.writeInt(0);
135             io.write(new byte[] {});
136         return this;
137     }
138 
139     public void close() {
140         if (this.byteInput != null) {
141             try {
142                 this.io.flush();
143             } catch (IOException e) {
144                 // close may not fail
145             }
146             IOUtils.closeQuietly(this.byteInput);
147         }
148     }
149 
150 }