1 24 25 package com.mckoi.util; 26 27 33 34 public final class ByteBuffer { 35 36 39 private byte[] buf; 40 41 44 private int pos; 45 46 49 private int lim; 50 51 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 67 public void position(int position) { 68 this.pos = position; 69 } 70 71 74 public int position() { 75 return pos; 76 } 77 78 81 public int limit() { 82 return lim; 83 } 84 85 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 101 public ByteBuffer put(ByteBuffer buffer) { 102 return put(buffer.buf, buffer.pos, buffer.lim); 103 } 104 105 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 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 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 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 |