1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import java.io.OutputStream;
25 import java.io.InputStream;
26 import java.io.DataOutputStream;
27 import java.io.DataInputStream;
28 import java.io.IOException;
29 
30 /**
31  *  <p>Contains the raw bytes for a record in a pdb.</p>
32  *
33  *  <p>Note that it is not associated with a record number or id.</p>
34  *
35  *  @author    Akhil Arora, Herbie Ong
36  *  @see    PalmDB
37  */
38 
39 
40 public final class Record {
41 
42     /** record bytes */
43     private byte[] data;
44 
45     /**
46      *  Default constructor.
47      */
48 
Record()49     public Record() {
50 
51         data = new byte[0];
52     }
53 
54     /**
55      *  Constructor to create a Record filled with bytes.
56      *  Note that this does not check for 64k record sizes.
57      *  User of this class has to check for that.
58      *
59      *  @param    d    byte array contents for this object.
60      */
61 
Record(byte[] d)62     public Record(byte[] d) {
63 
64         data = new byte[d.length];
65         System.arraycopy(d, 0, data, 0, d.length);
66     }
67 
68     /**
69      *  This method returns the number of bytes in this object.
70      *
71      *  @return    int    number of bytes in this object.
72      */
73 
getSize()74     public int getSize() {
75 
76         return data.length;
77     }
78 
79     /**
80      *  This method returns the contents of this object.
81      *
82      *  @return    byte[]    contents in byte array
83      */
84 
getBytes()85     public byte[] getBytes() {
86 
87         return data;
88     }
89 
90     /**
91      *  Write out the record length followed by the data
92      *  in this Record object.
93      *
94      *  @param   out    the stream to write the object to
95      *  @throws  IOException    if any I/O error occurs
96      */
97 
98 
write(OutputStream outs)99     public void write(OutputStream outs) throws IOException {
100 
101         DataOutputStream out = new DataOutputStream(outs);
102         out.writeShort(data.length);
103         out.write(data);
104     }
105 
106     /**
107      *  Read the necessary data to create a pdb from
108      *  the input stream.
109      *
110      *  @param   in    the stream to read data from in order to
111      *                 restore the object
112      *  @throws  IOException    if any I/O error occurs
113      */
114 
read(InputStream ins)115     public void read(InputStream ins) throws IOException {
116 
117         DataInputStream in = new DataInputStream(ins);
118         int len = in.readUnsignedShort();
119         data = new byte[len];
120         in.readFully(data);
121     }
122 
123     /**
124      *  Override equals method of Object.
125      *
126      *  2 Record objects are equal if they contain the same bytes
127      *  in the array.
128      *
129      *  This is used primarily for testing purposes only for now.
130      *
131      *  @param   obj    a Record object to compare with
132      *  @return   boolean    true if obj is equal to this, else false.
133      */
134 
equals(Object obj)135     public boolean equals(Object obj) {
136 
137         boolean bool = false;
138 
139         if (obj instanceof Record) {
140 
141             Record rec = (Record) obj;
142 
143             checkLabel: {
144 
145                 if (rec.getSize() == data.length) {
146 
147                     for (int i = 0; i < data.length; i++) {
148 
149                         if (data[i] != rec.data[i]) {
150                             break checkLabel;
151                         }
152                     }
153 
154                     bool = true;
155                 }
156             }
157         }
158 
159         return bool;
160     }
161 }
162