001: // HttpBuffer.java
002: // $Id: HttpBuffer.java,v 1.12 2004/01/13 12:36:12 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import java.io.IOException;
009: import java.io.OutputStream;
010:
011: /**
012: * A cool StringBuffer like class, for converting header values to String.
013: * Note that for good reasons, this class is <em>not</em> public.
014: */
015:
016: class HttpBuffer {
017: private static final int INIT_SIZE = 128;
018:
019: byte buf[] = null;
020: int len = 0;
021: byte sep = (byte) 0;
022:
023: final void append(byte b) {
024: ensureCapacity(1);
025: buf[len++] = b;
026: }
027:
028: final void append(char ch) {
029: append((byte) ch);
030: }
031:
032: final void append(int i) {
033: append((byte) i);
034: }
035:
036: final void appendLong(long i) {
037: appendLong(i, -1, (byte) 0);
038: }
039:
040: final void appendLong(long i, int padlen, byte pad) {
041: boolean neg = (i < 0);
042: int hackpos = len;
043: // Emit number in reverse order:
044: if (!neg)
045: i = -i;
046: while (i <= -10) {
047: append((byte) ('0' - (i % 10)));
048: padlen--;
049: i = i / 10;
050: }
051: append((byte) ('0' - i));
052: padlen--;
053: if (neg) {
054: append((byte) '-');
055: padlen--;
056: }
057: while (--padlen >= 0)
058: append(pad);
059: // Reverse byte order
060: int cnt = (len - hackpos) / 2;
061: int j = len - 1;
062: while (--cnt >= 0) {
063: int pos = hackpos + len - j - 1;
064: byte tmp = buf[j];
065: buf[j] = buf[pos];
066: buf[pos] = tmp;
067: j--;
068: }
069: }
070:
071: final void appendInt(int i) {
072: appendLong(i, -1, (byte) 0);
073: }
074:
075: final void appendInt(int i, int padlen, byte pad) {
076: appendLong(i, padlen, pad);
077: }
078:
079: final void ensureCapacity(int sz) {
080: int req = len + sz;
081: if (req >= buf.length) {
082: int nsz = buf.length << 1;
083: if (nsz < req)
084: nsz = req + 1;
085: byte nb[] = new byte[nsz];
086: System.arraycopy(buf, 0, nb, 0, len);
087: buf = nb;
088: }
089: return;
090: }
091:
092: void append(byte b[], int o, int l) {
093: ensureCapacity(l);
094: System.arraycopy(b, o, buf, len, l);
095: len += l;
096: }
097:
098: final void append(byte b[]) {
099: append(b, 0, b.length);
100: }
101:
102: void append(String str) {
103: int l = str.length();
104: ensureCapacity(l);
105: str.getBytes(0, l, buf, len);
106: len += l;
107: }
108:
109: void appendQuoted(String str) {
110: append((byte) '"');
111: append(str);
112: append((byte) '"');
113: }
114:
115: void append(String name, byte sep, String value) {
116: append(name);
117: append(sep);
118: append(value);
119: }
120:
121: void append(String name, byte sep, int value) {
122: append(name);
123: append(sep);
124: appendInt(value);
125: }
126:
127: void appendQuoted(String name, byte sep, String value) {
128: append(name);
129: append(sep);
130: append('"');
131: append(value);
132: append('"');
133: }
134:
135: void appendQuoted(String name, byte sep, String values[]) {
136: append(name);
137: if (values.length > 0) {
138: append(sep);
139: append((byte) '"');
140: for (int i = 0; i < values.length; i++) {
141: if (i > 0)
142: append(',');
143: append(values[i]);
144: }
145: append((byte) '"');
146: }
147: }
148:
149: void append(String name, byte sep, String values[]) {
150: append(name);
151: if (values.length > 0) {
152: append(sep);
153: for (int i = 0; i < values.length; i++) {
154: if (i > 0)
155: append(',');
156: append(values[i]);
157: }
158: }
159: }
160:
161: void append(double d) {
162: append(Double.toString(d));
163: }
164:
165: public String toString() {
166: return new String(buf, 0, 0, len);
167: }
168:
169: /**
170: * Get a copy of the current byte buffer.
171: */
172:
173: public byte[] getByteCopy() {
174: byte v[] = new byte[len];
175: System.arraycopy(buf, 0, v, 0, len);
176: return v;
177: }
178:
179: public final byte[] getBytes() {
180: return buf;
181: }
182:
183: public final int length() {
184: return len;
185: }
186:
187: public final void reset() {
188: len = 0;
189: }
190:
191: /**
192: * Emit the content of this byte buffer to the given output stream.
193: * @param out The output stream to emit the content to.
194: * @exception IOException If sone IO error occurs during emitting.
195: */
196:
197: public final void emit(OutputStream out) throws IOException {
198: if (out == null) {
199: throw new IOException("outputstream not existent");
200: }
201: out.write(buf, 0, len);
202: }
203:
204: HttpBuffer() {
205: this .buf = new byte[INIT_SIZE];
206: }
207:
208: HttpBuffer(int size) {
209: this .buf = new byte[size];
210: }
211: }
|