KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > Field


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.indexing;
12
13 public class Field implements Insertable {
14     protected Buffer buffer; // contents
15
protected int offset; // offset of the field within the buffer
16
protected int length; // length of the field
17

18     /**
19      * Constructor for a new Field.
20      */

21     public Field(byte[] bytes) {
22         this.buffer = new Buffer(bytes);
23         this.offset = 0;
24         this.length = bytes.length;
25     }
26
27     /**
28      * Constructor for a new Field.
29      */

30     public Field(int n) {
31         this.buffer = new Buffer(n);
32         this.offset = 0;
33         this.length = n;
34     }
35
36     /**
37      * Constructor for a new Field.
38      */

39     public Field(Buffer buffer, int offset, int length) {
40         this.buffer = buffer;
41         this.offset = offset;
42         this.length = length;
43     }
44
45     /**
46      * Constructor for a new Field.
47      */

48     public Field(Insertable anObject) {
49         buffer = new Buffer(anObject);
50         offset = 0;
51         length = buffer.length();
52     }
53
54     public Field clear() {
55         buffer.clear(offset, length);
56         return this;
57     }
58
59     public int compareTo(Field that) {
60         return Buffer.compare(this.buffer, this.offset, this.length, that.buffer, that.offset, that.length);
61     }
62
63     public Field subfield(int offset, int length) {
64         if (offset + length > this.length)
65             throw new IllegalArgumentException JavaDoc();
66         return buffer.getField(this.offset + offset, length);
67     }
68
69     public Field subfield(FieldDef d) {
70         if (d.offset + d.length > this.length)
71             throw new IllegalArgumentException JavaDoc();
72         return buffer.getField(this.offset + d.offset, d.length);
73     }
74
75     public Field subfield(int offset) {
76         return subfield(offset, this.length - offset);
77     }
78
79     public byte[] get() {
80         return buffer.get(offset, length);
81     }
82
83     public int getInt() {
84         return buffer.getInt(offset, length);
85     }
86
87     public long getLong() {
88         return buffer.getLong(offset, length);
89     }
90
91     public int getUInt() {
92         return buffer.getUInt(offset, length);
93     }
94
95     public byte[] get(FieldDef d) {
96         return subfield(d).get();
97     }
98
99     public int getInt(FieldDef d) {
100         return subfield(d).getInt();
101     }
102
103     public int length() {
104         return length;
105     }
106
107     public Pointer pointTo(int offset) {
108         return new Pointer(buffer, this.offset + offset);
109     }
110
111     public Field put(byte[] b) {
112         buffer.put(offset, length, b);
113         return this;
114     }
115
116     public Field put(int n) {
117         buffer.put(offset, length, n);
118         return this;
119     }
120
121     public Field put(long n) {
122         buffer.put(offset, length, n);
123         return this;
124     }
125
126     public Field put(Insertable anObject) {
127         put(anObject.toByteArray());
128         return this;
129     }
130
131     public Field put(FieldDef d, int n) {
132         subfield(d).put(n);
133         return this;
134     }
135
136     public Field put(FieldDef d, Insertable anObject) {
137         subfield(d).put(anObject.toByteArray());
138         return this;
139     }
140
141     /**
142      * Implementation of the Insertable interface.
143      */

144     public byte[] toByteArray() {
145         return get();
146     }
147
148 }
149
Popular Tags