1 17 18 package org.apache.geronimo.util.encoders; 19 20 21 25 public class BufferedEncoder 26 { 27 protected byte[] buf; 28 protected int bufOff; 29 30 protected Translator translator; 31 32 36 public BufferedEncoder( 37 Translator translator, 38 int bufSize) 39 { 40 this.translator = translator; 41 42 if ((bufSize % translator.getEncodedBlockSize()) != 0) 43 { 44 throw new IllegalArgumentException ("buffer size not multiple of input block size"); 45 } 46 47 buf = new byte[bufSize]; 48 bufOff = 0; 49 } 50 51 public int processByte( 52 byte in, 53 byte[] out, 54 int outOff) 55 { 56 int resultLen = 0; 57 58 buf[bufOff++] = in; 59 60 if (bufOff == buf.length) 61 { 62 resultLen = translator.encode(buf, 0, buf.length, out, outOff); 63 bufOff = 0; 64 } 65 66 return resultLen; 67 } 68 69 public int processBytes( 70 byte[] in, 71 int inOff, 72 int len, 73 byte[] out, 74 int outOff) 75 { 76 if (len < 0) 77 { 78 throw new IllegalArgumentException ("Can't have a negative input length!"); 79 } 80 81 int resultLen = 0; 82 int gapLen = buf.length - bufOff; 83 84 if (len > gapLen) 85 { 86 System.arraycopy(in, inOff, buf, bufOff, gapLen); 87 88 resultLen += translator.encode(buf, 0, buf.length, out, outOff); 89 90 bufOff = 0; 91 92 len -= gapLen; 93 inOff += gapLen; 94 outOff += resultLen; 95 96 int chunkSize = len - (len % buf.length); 97 98 resultLen += translator.encode(in, inOff, chunkSize, out, outOff); 99 100 len -= chunkSize; 101 inOff += chunkSize; 102 } 103 104 if (len != 0) 105 { 106 System.arraycopy(in, inOff, buf, bufOff, len); 107 108 bufOff += len; 109 } 110 111 return resultLen; 112 } 113 } 114 | Popular Tags |