?? record.java
字號(hào):
package com.jmobilecore.dstore;
/**
* A <code>Record</code> represents a database record
*
* @author Greg Gridin
*/
public class Record {
/**
* The maximum size for the record's data
**/
public static int MAX_SIZE = 1024;
/**
* The <code>RecordStructure</code> for the current <code>Record</code>
**/
public RecordStructure struct;
/**
* The data for the current <code>Record</code>
**/
public byte[] data;
/**
* The length of the data
**/
public int length;
/**
* Creates an <code>Record</code> instance
* @param rStructure the <code>RecordStructure</code> for the record.
**/
public Record(RecordStructure rStructure) {
struct = rStructure;
data = new byte[MAX_SIZE];
if (rStructure == null) return;
reset();
}
/**
* Creates an <code>Record</code> instance
* @param rStructure the <code>RecordStructure</code> for the record.
* @param data the input data for the record.
**/
public Record(RecordStructure rStructure, byte[] data) {
struct = rStructure;
this.data = data;
calcLength();
}
protected void calcLength() {
if (struct == null) {
length = 0;
return;
}
int numFields = struct.fieldStructure.size();
FieldStructure fStruct;
int newLength = numFields * 4;
for (int i = 0; i < numFields; i++) {
fStruct = struct.getFieldStructure(i);
if (FieldStructure.hasVariableLength(fStruct.type)) {
int desc = getDescriptor(i);
if (desc == FieldStructure.NULL) continue;
int offset = desc >>> 16;
int len = (desc & 0x0000ffff);
if (newLength < offset + len) newLength = offset + len;
}
}
length = newLength;
}
/**
* The buffer used for current <code>Record</code> compacting
**/
static protected Record buffer = new Record(null);
protected void compact() {
synchronized (buffer) {
buffer.struct = struct;
int newOffset = struct.descLength;
FieldStructure field;
int desc, curOffset, curLength;
for (int i = 0; i < struct.fieldStructure.size(); i++) {
field = struct.getFieldStructure(i);
desc = getDescriptor(i);
if (!FieldStructure.hasVariableLength(field.type) || desc == FieldStructure.NULL) {
buffer.setDescriptor(i, desc);
continue;
}
curOffset = desc >>> 16;
curLength = (desc & 0x0000ffff);
buffer.setDescriptor(i, newOffset << 16 | curLength);
if (curLength<=0) continue;
System.arraycopy(data, curOffset, buffer.data, newOffset, curLength);
newOffset += curLength;
}
if (data.length < newOffset) {
data = new byte[MAX_SIZE];
}
System.arraycopy(buffer.data, 0, data, 0, newOffset);
length = newOffset;
}
}
/**
* Sets specified column to <code>byte[]</code> value
* Compacts storage if necessary
* @param fIndex the field index.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field index was given
* @exception ArrayStoreException if an invalid value cannot be stored in <code>Record</code>
* @see FieldStructure#NULL
**/
protected void setBinaryData(int fIndex, byte[] value, int offset, int numBytes) {
if (!FieldStructure.hasVariableLength(struct.getFieldStructure(fIndex).type)) {
return;
}
int curOffset = length, curLength = 0;
int desc = getDescriptor(fIndex);
if (desc != FieldStructure.NULL) {
curOffset = desc >>> 16;
curLength = (desc & 0x0000ffff);
}
if (numBytes==0) {
setDescriptor(fIndex, curOffset << 16 | numBytes);
return;
}
int dynamicSize = data.length - length - (numBytes - curLength);
if (dynamicSize < 0) {
compact();
desc = getDescriptor(fIndex);
if (desc == FieldStructure.NULL) curOffset = length;
else curOffset = desc >>> 16;
dynamicSize = data.length - length - (numBytes - curLength);
}
if (dynamicSize < 0) {
throw new ArrayStoreException();
}
setDescriptor(fIndex, curOffset << 16 | numBytes);
if (curOffset != length) {
if (curLength < numBytes) {
compact();
desc = getDescriptor(fIndex);
curOffset = desc >>> 16;
}
}
else {
length += numBytes - curLength;
}
System.arraycopy(value, offset, data, curOffset, numBytes);
}
/**
* Sets specified column to <code>byte[]</code> value
* @param fName the field name.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field name was given
* @see FieldStructure#NULL
**/
public void setBinaryField(String fName, byte[] value, int offset, int numBytes) {
setBinaryField(struct.getFieldNum(fName), value, offset, numBytes);
}
/**
* Sets specified column to <code>byte[]</code> value
* @param fIndex the field index.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field index was given
* @see FieldStructure#NULL
**/
public void setBinaryField(int fIndex, byte[] value, int offset, int numBytes) {
if (value == null) {
setNull(fIndex);
return;
}
setBinaryData(fIndex, value, offset, numBytes);
}
/**
* Sets specified column to <code>String</code> value
* @param fName the field name.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field name was given
* @see FieldStructure#NULL
**/
public void setStringField(String fName, String value) {
setStringField(struct.getFieldNum(fName), value);
}
/**
* Sets specified column to <code>String</code> value
* @param fIndex the field index.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field index was given
* @see FieldStructure#NULL
**/
public void setStringField(int fIndex, String value) {
if (value == null) {
setNull(fIndex);
return;
}
byte [] data = value.getBytes();
setBinaryData(fIndex, data, 0, data.length);
data = null;
}
/**
* Sets specified column to <code>boolean</code> value
* @param fName the field name.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field name was given
* @see FieldStructure#NULL
**/
public void setBooleanField(String fName, boolean value) {
setBooleanField(struct.getFieldNum(fName), value);
}
/**
* Sets specified column to <code>boolean</code> value
* @param fIndex the field index.
* @param value the field value.
* @exception IndexOutOfBoundsException if an invalid field index was given
* @see FieldStructure#NULL
**/
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -