1 19 20 21 package org.apache.slide.webdav.logger; 22 23 import java.io.IOException ; 24 25 import org.apache.slide.common.Domain; 26 27 43 44 45 public class XByteBuffer { 46 48 protected static final int DEFAULT_BUFFER_SIZE = 8*1024; 49 int defaultBufferSize = DEFAULT_BUFFER_SIZE; 50 int bytesWritten = 0; 51 52 54 private byte buf[]; 55 private byte buf2[]; 56 57 61 public int count = 0; 62 64 65 public XByteBuffer() { 66 buf = new byte[defaultBufferSize]; 67 } 68 69 public void write(int b) throws IOException { 70 if (count >= buf.length) { 71 resize(); 72 } 73 buf[count++] = (byte)b; 74 bytesWritten++; 75 } 76 77 public void write(byte b[], int off, int len) throws IOException { 78 int avail = buf.length - count; 79 Domain.debug("ENTER: XByteBuffer:write(len="+len+") avail="+avail); 80 81 if ( len > avail ) { 82 int newLength = buf.length; 83 do { 84 newLength = newLength + newLength / 2; 85 } while ( newLength <= count + len ); 86 resize( newLength ); 87 } 88 89 if ( len > 0 ) { 90 System.arraycopy(b, off, buf, count, len); 91 count += len; 92 bytesWritten += len; 93 } 94 Domain.debug("LEAVE: XByteBuffer:write(len="+len+") bytesWritten="+bytesWritten); 95 return; 96 } 97 98 102 private void resize() { 103 resize( buf.length + buf.length / 2 ); 104 } 105 106 110 private void resize( int neededSize ) { 111 Domain.debug("XByteBuffer:resize( neededSize="+neededSize+")"); 112 buf2 = new byte[neededSize]; 113 System.arraycopy( buf, 0, buf2, 0, buf.length ); 114 buf = buf2; 115 buf2 = null; 116 } 117 118 public boolean isContentWritten() { 119 return bytesWritten!=0; 120 } 121 122 public void setBufferSize(int size) { 123 if( size > buf.length ) { 124 buf=new byte[size]; 125 } 126 } 127 128 public int getBufferSize() { 129 return buf.length; 130 } 131 132 133 135 public byte[] getBuffer() { 136 return buf; 137 } 138 139 public String getBufferContent() { 140 return new String ( buf ).substring( 0, bytesWritten ); 141 } 143 144 public int getNumberOfBytesWritten() { 145 return bytesWritten; 146 } 147 148 } 149 | Popular Tags |