1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.jk.core;
19
20 import java.io.IOException;
21
22 import org.apache.tomcat.util.buf.ByteChunk;
23 import org.apache.tomcat.util.buf.MessageBytes;
24
25
26 /**
27 * A single packet for communication between the web server and the
28 * container.
29 *
30 * In a more generic sense, it's the event that drives the processing chain.
31 * XXX Use Event, make Msg a particular case.
32 *
33 * @author Henri Gomez [hgomez@apache.org]
34 * @author Dan Milstein [danmil@shore.net]
35 * @author Keith Wannamaker [Keith@Wannamaker.org]
36 * @author Kevin Seguin
37 * @author Costin Manolache
38 */
39 public abstract class Msg {
40
41
42
43 /**
44 * Prepare this packet for accumulating a message from the container to
45 * the web server. Set the write position to just after the header
46 * (but leave the length unwritten, because it is as yet unknown).
47 */
48 public abstract void reset();
49
50 /**
51 * For a packet to be sent to the web server, finish the process of
52 * accumulating data and write the length of the data payload into
53 * the header.
54 */
55 public abstract void end();
56
57 public abstract void appendInt( int val );
58
59 public abstract void appendByte( int val );
60
61 public abstract void appendLongInt( int val );
62
63 /**
64 */
65 public abstract void appendBytes(MessageBytes mb) throws IOException;
66
67 public abstract void appendByteChunk(ByteChunk bc) throws IOException;
68
69 /**
70 * Copy a chunk of bytes into the packet, starting at the current
71 * write position. The chunk of bytes is encoded with the length
72 * in two bytes first, then the data itself, and finally a
73 * terminating \0 (which is <B>not</B> included in the encoded
74 * length).
75 *
76 * @param b The array from which to copy bytes.
77 * @param off The offset into the array at which to start copying
78 * @param numBytes The number of bytes to copy.
79 */
80 public abstract void appendBytes( byte b[], int off, int numBytes );
81
82 /**
83 * Read an integer from packet, and advance the read position past
84 * it. Integers are encoded as two unsigned bytes with the
85 * high-order byte first, and, as far as I can tell, in
86 * little-endian order within each byte.
87 */
88 public abstract int getInt();
89
90 public abstract int peekInt();
91
92 public abstract byte getByte();
93
94 public abstract byte peekByte();
95
96 public abstract void getBytes(MessageBytes mb);
97
98 /**
99 * Copy a chunk of bytes from the packet into an array and advance
100 * the read position past the chunk. See appendBytes() for details
101 * on the encoding.
102 *
103 * @return The number of bytes copied.
104 */
105 public abstract int getBytes(byte dest[]);
106
107 /**
108 * Read a 32 bits integer from packet, and advance the read position past
109 * it. Integers are encoded as four unsigned bytes with the
110 * high-order byte first, and, as far as I can tell, in
111 * little-endian order within each byte.
112 */
113 public abstract int getLongInt();
114
115 public abstract int getHeaderLength();
116
117 public abstract int processHeader();
118
119 public abstract byte[] getBuffer();
120
121 public abstract int getLen();
122
123 public abstract void dump(String msg);
124
125 /* -------------------- Utilities -------------------- */
126 // XXX Move to util package
127
128 public static String hexLine( byte buf[], int start, int len ) {
129 StringBuffer sb=new StringBuffer();
130 for( int i=start; i< start+16 ; i++ ) {
131 if( i < len + 4)
132 sb.append( hex( buf[i] ) + " ");
133 else
134 sb.append( " " );
135 }
136 sb.append(" | ");
137 for( int i=start; i < start+16 && i < len + 4; i++ ) {
138 if( ! Character.isISOControl( (char)buf[i] ))
139 sb.append( new Character((char)buf[i]) );
140 else
141 sb.append( "." );
142 }
143 return sb.toString();
144 }
145
146 private static String hex( int x ) {
147 // if( x < 0) x=256 + x;
148 String h=Integer.toHexString( x );
149 if( h.length() == 1 ) h = "0" + h;
150 return h.substring( h.length() - 2 );
151 }
152
153
154 }