01: package org.jgroups.util;
02:
03: /**
04: * Buffer with an offset and length. Will be replaced with NIO equivalent once JDK 1.4 becomes baseline
05: * @author Bela Ban
06: * @version $Id: Buffer.java,v 1.4 2005/09/06 09:53:53 belaban Exp $
07: */
08: public class Buffer {
09: byte[] buf;
10: int offset;
11: int length;
12:
13: public Buffer(byte[] buf, int offset, int length) {
14: this .buf = buf;
15: this .offset = offset;
16: this .length = length;
17: }
18:
19: public byte[] getBuf() {
20: return buf;
21: }
22:
23: public void setBuf(byte[] buf) {
24: this .buf = buf;
25: }
26:
27: public int getOffset() {
28: return offset;
29: }
30:
31: public void setOffset(int offset) {
32: this .offset = offset;
33: }
34:
35: public int getLength() {
36: return length;
37: }
38:
39: public void setLength(int length) {
40: this .length = length;
41: }
42:
43: public Buffer copy() {
44: byte[] new_buf = buf != null ? new byte[length] : null;
45: int new_length = new_buf != null ? new_buf.length : 0;
46: if (new_buf != null)
47: System.arraycopy(buf, offset, new_buf, 0, length);
48: return new Buffer(new_buf, 0, new_length);
49: }
50:
51: public String toString() {
52: StringBuffer sb = new StringBuffer();
53: sb.append(length).append(" bytes");
54: if (offset > 0)
55: sb.append(" (offset=").append(offset).append(")");
56: return sb.toString();
57: }
58:
59: }
|