KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > ByteBuffer


1 /**
2  * com.mckoi.util.ByteBuffer 19 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 /**
28  * A wrapper for an array of byte[]. This provides various functions for
29  * altering the state of the buffer.
30  *
31  * @author Tobias Downer
32  */

33
34 public final class ByteBuffer {
35
36   /**
37    * The byte[] array itself.
38    */

39   private byte[] buf;
40
41   /**
42    * The current position in the array.
43    */

44   private int pos;
45
46   /**
47    * The length of the buf array.
48    */

49   private int lim;
50
51   /**
52    * Constructs the buffer.
53    */

54   public ByteBuffer(byte[] buf, int offset, int length) {
55     this.buf = buf;
56     this.lim = length;
57     this.pos = offset;
58   }
59
60   public ByteBuffer(byte[] buf) {
61     this(buf, 0, buf.length);
62   }
63
64   /**
65    * Sets the position in to the buffer.
66    */

67   public void position(int position) {
68     this.pos = position;
69   }
70
71   /**
72    * Returns the current position.
73    */

74   public int position() {
75     return pos;
76   }
77
78   /**
79    * Returns the limit of this buffer.
80    */

81   public int limit() {
82     return lim;
83   }
84
85   /**
86    * Puts a byte array into the buffer.
87    */

88   public ByteBuffer put(byte[] b, int offset, int length) {
89     System.arraycopy(b, offset, buf, pos, length);
90     position(pos + length);
91     return this;
92   }
93
94   public ByteBuffer put(byte[] b) {
95     return put(b, 0, b.length);
96   }
97
98   /**
99    * Puts a ByteBuffer in to this buffer.
100    */

101   public ByteBuffer put(ByteBuffer buffer) {
102     return put(buffer.buf, buffer.pos, buffer.lim);
103   }
104
105   /**
106    * Gets a byte array from the buffer.
107    */

108   public ByteBuffer get(byte[] b, int offset, int length) {
109     System.arraycopy(buf, pos, b, offset, length);
110     position(pos + length);
111     return this;
112   }
113
114   /**
115    * Puts/Gets an integer into the buffer at the current position.
116    */

117   public ByteBuffer putInt(int v) {
118     ByteArrayUtil.setInt(v, buf, pos);
119     position(pos + 4);
120     return this;
121   }
122
123   public int getInt() {
124     int v = ByteArrayUtil.getInt(buf, pos);
125     position(pos + 4);
126     return v;
127   }
128
129   /**
130    * Puts/Gets a byte into the buffer at the current position.
131    */

132   public ByteBuffer putByte(byte v) {
133     buf[pos] = v;
134     ++pos;
135     return this;
136   }
137
138   public byte getByte() {
139     byte b = buf[pos];
140     ++pos;
141     return b;
142   }
143
144   /**
145    * Puts/Gets a short into the buffer at the current position.
146    */

147   public ByteBuffer putShort(short v) {
148     ByteArrayUtil.setShort(v, buf, pos);
149     position(pos + 2);
150     return this;
151   }
152
153   public short getShort() {
154     short v = ByteArrayUtil.getShort(buf, pos);
155     position(pos + 2);
156     return v;
157   }
158
159
160 }
161
Popular Tags