001: /**
002: * com.mckoi.util.ByteBuffer 19 Sep 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: /**
026: * A wrapper for an array of byte[]. This provides various functions for
027: * altering the state of the buffer.
028: *
029: * @author Tobias Downer
030: */
031:
032: public final class ByteBuffer {
033:
034: /**
035: * The byte[] array itself.
036: */
037: private byte[] buf;
038:
039: /**
040: * The current position in the array.
041: */
042: private int pos;
043:
044: /**
045: * The length of the buf array.
046: */
047: private int lim;
048:
049: /**
050: * Constructs the buffer.
051: */
052: public ByteBuffer(byte[] buf, int offset, int length) {
053: this .buf = buf;
054: this .lim = length;
055: this .pos = offset;
056: }
057:
058: public ByteBuffer(byte[] buf) {
059: this (buf, 0, buf.length);
060: }
061:
062: /**
063: * Sets the position in to the buffer.
064: */
065: public void position(int position) {
066: this .pos = position;
067: }
068:
069: /**
070: * Returns the current position.
071: */
072: public int position() {
073: return pos;
074: }
075:
076: /**
077: * Returns the limit of this buffer.
078: */
079: public int limit() {
080: return lim;
081: }
082:
083: /**
084: * Puts a byte array into the buffer.
085: */
086: public ByteBuffer put(byte[] b, int offset, int length) {
087: System.arraycopy(b, offset, buf, pos, length);
088: position(pos + length);
089: return this ;
090: }
091:
092: public ByteBuffer put(byte[] b) {
093: return put(b, 0, b.length);
094: }
095:
096: /**
097: * Puts a ByteBuffer in to this buffer.
098: */
099: public ByteBuffer put(ByteBuffer buffer) {
100: return put(buffer.buf, buffer.pos, buffer.lim);
101: }
102:
103: /**
104: * Gets a byte array from the buffer.
105: */
106: public ByteBuffer get(byte[] b, int offset, int length) {
107: System.arraycopy(buf, pos, b, offset, length);
108: position(pos + length);
109: return this ;
110: }
111:
112: /**
113: * Puts/Gets an integer into the buffer at the current position.
114: */
115: public ByteBuffer putInt(int v) {
116: ByteArrayUtil.setInt(v, buf, pos);
117: position(pos + 4);
118: return this ;
119: }
120:
121: public int getInt() {
122: int v = ByteArrayUtil.getInt(buf, pos);
123: position(pos + 4);
124: return v;
125: }
126:
127: /**
128: * Puts/Gets a byte into the buffer at the current position.
129: */
130: public ByteBuffer putByte(byte v) {
131: buf[pos] = v;
132: ++pos;
133: return this ;
134: }
135:
136: public byte getByte() {
137: byte b = buf[pos];
138: ++pos;
139: return b;
140: }
141:
142: /**
143: * Puts/Gets a short into the buffer at the current position.
144: */
145: public ByteBuffer putShort(short v) {
146: ByteArrayUtil.setShort(v, buf, pos);
147: position(pos + 2);
148: return this ;
149: }
150:
151: public short getShort() {
152: short v = ByteArrayUtil.getShort(buf, pos);
153: position(pos + 2);
154: return v;
155: }
156:
157: }
|