KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > core > Msg


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

16
17 package org.apache.jk.core;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.tomcat.util.buf.ByteChunk;
22 import org.apache.tomcat.util.buf.MessageBytes;
23
24
25 /**
26  * A single packet for communication between the web server and the
27  * container.
28  *
29  * In a more generic sense, it's the event that drives the processing chain.
30  * XXX Use Event, make Msg a particular case.
31  *
32  * @author Henri Gomez [hgomez@apache.org]
33  * @author Dan Milstein [danmil@shore.net]
34  * @author Keith Wannamaker [Keith@Wannamaker.org]
35  * @author Kevin Seguin
36  * @author Costin Manolache
37  */

38 public abstract class Msg {
39
40     
41     
42     /**
43      * Prepare this packet for accumulating a message from the container to
44      * the web server. Set the write position to just after the header
45      * (but leave the length unwritten, because it is as yet unknown).
46      */

47     public abstract void reset();
48
49     /**
50      * For a packet to be sent to the web server, finish the process of
51      * accumulating data and write the length of the data payload into
52      * the header.
53      */

54     public abstract void end();
55
56     public abstract void appendInt( int val );
57
58     public abstract void appendByte( int val );
59     
60     public abstract void appendLongInt( int val );
61
62     /**
63      */

64     public abstract void appendBytes(MessageBytes mb) throws IOException JavaDoc;
65
66     public abstract void appendByteChunk(ByteChunk bc) throws IOException JavaDoc;
67     
68     /**
69      * Copy a chunk of bytes into the packet, starting at the current
70      * write position. The chunk of bytes is encoded with the length
71      * in two bytes first, then the data itself, and finally a
72      * terminating \0 (which is <B>not</B> included in the encoded
73      * length).
74      *
75      * @param b The array from which to copy bytes.
76      * @param off The offset into the array at which to start copying
77      * @param numBytes The number of bytes to copy.
78      */

79     public abstract void appendBytes( byte b[], int off, int numBytes );
80
81     /**
82      * Read an integer from packet, and advance the read position past
83      * it. Integers are encoded as two unsigned bytes with the
84      * high-order byte first, and, as far as I can tell, in
85      * little-endian order within each byte.
86      */

87     public abstract int getInt();
88
89     public abstract int peekInt();
90
91     public abstract byte getByte();
92
93     public abstract byte peekByte();
94
95     public abstract void getBytes(MessageBytes mb);
96     
97     /**
98      * Copy a chunk of bytes from the packet into an array and advance
99      * the read position past the chunk. See appendBytes() for details
100      * on the encoding.
101      *
102      * @return The number of bytes copied.
103      */

104     public abstract int getBytes(byte dest[]);
105
106     /**
107      * Read a 32 bits integer from packet, and advance the read position past
108      * it. Integers are encoded as four unsigned bytes with the
109      * high-order byte first, and, as far as I can tell, in
110      * little-endian order within each byte.
111      */

112     public abstract int getLongInt();
113
114     public abstract int getHeaderLength();
115
116     public abstract int processHeader();
117
118     public abstract byte[] getBuffer();
119
120     public abstract int getLen();
121     
122     public abstract void dump(String JavaDoc msg);
123
124     /* -------------------- Utilities -------------------- */
125     // XXX Move to util package
126

127     public static String JavaDoc hexLine( byte buf[], int start, int len ) {
128         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
129         for( int i=start; i< start+16 ; i++ ) {
130             if( i < len + 4)
131                 sb.append( hex( buf[i] ) + " ");
132             else
133                 sb.append( " " );
134         }
135         sb.append(" | ");
136         for( int i=start; i < start+16 && i < len + 4; i++ ) {
137             if( ! Character.isISOControl( (char)buf[i] ))
138                 sb.append( new Character JavaDoc((char)buf[i]) );
139             else
140                 sb.append( "." );
141         }
142         return sb.toString();
143     }
144
145     private static String JavaDoc hex( int x ) {
146         // if( x < 0) x=256 + x;
147
String JavaDoc h=Integer.toHexString( x );
148         if( h.length() == 1 ) h = "0" + h;
149         return h.substring( h.length() - 2 );
150     }
151
152
153 }
154
Popular Tags