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  import org.csc.phynixx.common.exceptions.DelegatedRuntimeException;
26  
27  import java.io.*;
28  
29  /**
30   * Created by christoph on 12.01.14.
31   */
32  public class LogRecordReader {
33  
34      private final ByteArrayInputStream byteInput;
35  
36      private final DataInputStream io;
37  
38      public LogRecordReader(byte[] content) {
39          this.byteInput = new ByteArrayInputStream(content);
40          this.io = new DataInputStream(this.byteInput);
41  
42      }
43  
44      public String readUTF() throws IOException {
45          return this.io.readUTF();
46      }
47  
48      public long readLong() throws IOException {
49          return this.io.readLong();
50      }
51      public byte readByte() throws IOException {
52          return this.io.readByte();
53      }
54  
55  
56      public boolean readBoolean() throws IOException {
57          return this.io.readBoolean();
58      }
59  
60      public int readInt() throws IOException {
61          return this.io.readInt();
62      }
63  
64  
65      public float readFloat() throws IOException {
66          return this.io.readFloat();
67      }
68  
69      public double readDouble() throws IOException {
70          return this.io.readDouble();
71      }
72  
73      public short readShort() throws IOException {
74          return io.readShort();
75      }
76  
77      public Object readObject() throws IOException {
78          ObjectInputStream in=null;
79          try {
80              int byteLength= io.readInt();
81              byte[] byteArray= new byte[byteLength];
82              io.readFully(byteArray);
83              if(byteArray.length==0) {
84                  return null;
85              }
86              in= new ObjectInputStream(new ByteArrayInputStream(byteArray));
87              try {
88                  return in.readObject();
89              } catch (ClassNotFoundException e) {
90                  throw new DelegatedRuntimeException("Error restoring Serialiazable", e);
91              }
92          } finally {
93              if(in!=null) {
94                  IOUtils.closeQuietly(in);
95              }
96          }
97      }
98  
99  
100     public void close() {
101         if (this.byteInput != null) {
102             IOUtils.closeQuietly(this.byteInput);
103         }
104     }
105 
106 
107 }