⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recordstructure.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 JAVA
字号:
package com.jmobilecore.dstore;

import java.util.Vector;

/**
 * A <code>RecordStructure</code> represents a description of a database record
 *
 * @author 	Greg Gridin
 */
public class RecordStructure {

    /**
     * A <code>Vector</code> of field structures in the current <code>RecordStructure</code>
     */
    public Vector fieldStructure;

    /**
     * Total length of all descriptor fields
     */
    public int descLength;

    /**
     * Creates new <code>RecordStructure</code> of specified size
     * @param fieldsNum number of fields in the record.
     */
    public RecordStructure(int fieldsNum) {
        fieldStructure = new Vector(fieldsNum);
    }

    /**
     * Creates new <code>RecordStructure</code> from binary form
     * @param binaryForm binary representation of <code>RecordStructure</code>
     * @see #toByteArray()
     */
    public RecordStructure(byte[] binaryForm) {
        int fieldNum = binaryForm[0];
        fieldStructure = new Vector(fieldNum);
        int counter = 1;
        int type;
        int byteLen;
        String name;
        for (int i=0; i<fieldNum; i++) {
            type = binaryForm[counter++];
            byteLen = binaryForm[counter++];
            name = new String(binaryForm, counter, byteLen);
            counter +=byteLen;
            addField(name, type);
        }
    }

    /**
     * Export current <code>RecordStructure</code> to binary form
     * @return binary representation of <code>RecordStructure</code>
     * @see #RecordStructure(byte[] binaryForm)
     */
    public byte[] toByteArray() {
        int counter = fieldStructure.size()*2+1;
        for (int i=0; i<fieldStructure.size(); i++) {
            counter += ((FieldStructure)fieldStructure.elementAt(i)).name.getBytes().length;
        }
        byte[] rslt = new byte[counter];
        counter = 0;
        rslt[counter++] = (byte)fieldStructure.size();
        byte[] fieldName;
        for (int i=0; i<fieldStructure.size(); i++) {
            FieldStructure field = (FieldStructure)fieldStructure.elementAt(i);
            rslt[counter++] = (byte)field.type;
            fieldName = field.name.getBytes();
            rslt[counter++] = (byte)fieldName.length;
            System.arraycopy(fieldName, 0, rslt, counter, fieldName.length);
            counter += fieldName.length;
        }
        return rslt;
    }

    /**
     * Add new record field to the <code>RecordStructure</code>
     * @param  fName the field name.
     * @param  fType the field type.
     * @return <code>true</code> if field was successfully added to the record, <code>false</code> otherwise
     */
    public boolean addField(String fName, int fType) {

        return addField(new FieldStructure(fName, fType));
    }

    /**
     * Add new record field to the <code>RecordStructure</code>
     * @param  fStruct the field structure.
     * @return <code>true</code> if field was successfully added to the record, <code>false</code> otherwise
     */
    public boolean addField(FieldStructure fStruct) {

        if (fStruct==null) return false;
        fieldStructure.addElement(fStruct);
        descLength += 4;
        return true;
    }

    /**
     * Get field number for specified field name
     * @param  fName the field name.
     * @return the non-negative field number, <code>-1</code> if field name was not found
     */
    public int getFieldNum(String fName) {

        FieldStructure fs;
        final int fSize = fieldStructure.size();
        for (int i = 0; i < fSize; i++) {
            fs = (FieldStructure) fieldStructure.elementAt(i);
            if (fs.name.compareTo(fName) == 0) {
                return i;
            }
        }
        return Table.INVALID_RECORD_ID;
    }

    /**
     * Get field structure for specified field name
     * @param  fName the field name.
     * @return the field structure found, <code>null</code> otherwise
     */
    public FieldStructure getFieldStructure(String fName) {

        return getFieldStructure(getFieldNum(fName));
    }

    /**
     * Get field structure for specified field number
     * @param  fIndex the field index, the first field is 0, second is 1.
     * @return the field structure found, null otherwise
     */
    public FieldStructure getFieldStructure(int fIndex) {

        if (fIndex < 0 || fIndex >= fieldStructure.size())
            return null;
        return (FieldStructure) fieldStructure.elementAt(fIndex);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -