KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > logger > XByteBuffer


1 /*
2  * ====================================================================
3  *
4  * Copyright 1999-2002 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20
21 package org.apache.slide.webdav.logger;
22
23 import java.io.IOException JavaDoc;
24
25 import org.apache.slide.common.Domain;
26
27 /**
28  * Un-synchronized byte buffer. We have methods to write and read from the
29  * buffer, and helpers can convert various data formats.
30  *
31  * The idea is to minimize the number of buffers and the amount of copy from
32  * layer to layer. It's _not_ premature optimization - it's the way things
33  * should work.
34  *
35  * The Request and Response will use several buffers, same for the protocol
36  * adapters.
37  *
38  * Note that the Buffer owns his byte[], while the Chunk is just a light
39  * cursor.
40  *
41  *
42  */

43
44
45 public class XByteBuffer {
46     // everything happens inside one thread !!!
47

48     protected static final int DEFAULT_BUFFER_SIZE = 8*1024;
49     int defaultBufferSize = DEFAULT_BUFFER_SIZE;
50     int bytesWritten = 0;
51
52     /** The buffers
53      */

54     private byte buf[];
55     private byte buf2[];
56
57     /**
58      * The index one greater than the index of the last valid byte in
59      * the buffer.
60      */

61     public int count = 0;
62     // count==-1 for end of stream
63

64
65     public XByteBuffer() {
66         buf = new byte[defaultBufferSize];
67     }
68
69     public void write(int b) throws IOException JavaDoc {
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 JavaDoc {
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     /**
99      * enlarge the buffer.
100      * @pre buf != null
101      */

102     private void resize() {
103         resize( buf.length + buf.length / 2 );
104     }
105
106     /**
107      * enlarge the buffer.
108      * @pre buf != null
109      */

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     // -------------------- Utils
134

135     public byte[] getBuffer() {
136         return buf;
137     }
138
139     public String JavaDoc getBufferContent() {
140         return new String JavaDoc( buf ).substring( 0, bytesWritten );
141 //hak return new String( buf );
142
}
143
144     public int getNumberOfBytesWritten() {
145         return bytesWritten;
146     }
147     
148 }
149
Popular Tags