1 7 package com.ibm.icu.impl; 9 10 13 public final class ByteBuffer { 14 private byte[] data; 15 16 private int pos; 17 18 private int limit; 19 20 private ByteBuffer() { 21 } 22 23 public byte[] array() { 24 byte[] result = new byte[limit]; 25 for (int i = 0; i < limit; ++i) { 26 result[i] = data[i]; 27 } 28 return result; 29 } 30 31 public static ByteBuffer wrap(byte[] data) { 32 if (data == null) 33 throw new NullPointerException (); 34 ByteBuffer result = new ByteBuffer(); 35 result.data = data; 36 result.pos = 0; 37 result.limit = data.length; 38 return result; 39 } 40 41 public int limit() { 42 return limit; 43 } 44 45 public int remaining() { 46 return limit - pos; 47 } 48 49 public byte get() { 50 if (pos < limit) 51 return data[pos++]; 52 throw new IndexOutOfBoundsException (); 53 } 54 55 public void get(byte[] dst, int offset, int length) { 56 if (offset < 0 || offset + length > dst.length || pos + length > limit) { 57 throw new IndexOutOfBoundsException (); 58 } 59 for (int i = 0; i < length; ++i) { 60 dst[offset++] = data[pos++]; 61 } 62 } 63 public static final ByteBuffer allocate(int size){ 64 ByteBuffer ret = new ByteBuffer(); 65 ret.data = new byte[size]; 66 return ret; 67 } 68 } 69 | Popular Tags |