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