1 package org.csc.phynixx.common.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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 }