1 package net.matuschek.util; 2 3 6 7 15 public class ByteBuffer { 16 17 protected final int INITIALSIZE=1024; 18 19 protected int used = 0; 20 protected int size = 0; 21 protected byte[] buff =null; 22 23 30 public ByteBuffer() { 31 size=INITIALSIZE; 32 buff=new byte[INITIALSIZE]; 33 } 34 35 36 48 public void append(byte b) { 49 if (used >= size) { 50 doubleBuffer(); 51 } 52 53 buff[used]=b; 54 used++; 55 } 56 57 60 public int length() { 61 return used; 62 } 63 64 65 68 public byte[] getContent() { 69 byte[] b = new byte[used]; 70 for (int i=0; i<used; i++) { 71 b[i]=buff[i]; 72 } 73 return b; 74 } 75 76 79 public void clean() { 80 used=0; 81 } 82 83 84 90 public void setSize(int size) { 91 92 if (size < used) { 94 return; 95 } 96 97 this.size=size; 98 99 byte[] newBuff = new byte[size]; 101 102 for (int i=0; i<used; i++) { 104 newBuff[i]=buff[i]; 105 } 106 107 buff=newBuff; 108 } 109 110 111 115 public String toString() { 116 StringBuffer sb = new StringBuffer (buff.length); 117 for (int i=0; i<used; i++) { 118 sb.append(buff[i]); 119 } 120 return sb.toString(); 121 } 122 123 124 127 protected void doubleBuffer() { 128 setSize(size*2); 130 } 131 132 133 134 } 135 | Popular Tags |