# Class AjpMessage

java.lang.Object
org.apache.coyote.ajp.AjpMessage

public class AjpMessage extends Object
A single packet for communication between the web server and the container. Designed to be reused many times with no creation of garbage. Understands the format of data types for these packets. Can be used (somewhat confusingly) for both incoming and outgoing packets.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final byte[]`
`buf`
Fixed size buffer.
`protected int`
`len`
This actually means different things depending on whether the packet is read or write.
`protected int`
`pos`
The current read or write position in the buffer.
`protected static final StringManager`
`sm`
The string manager for this package.

## Constructor Summary

Constructors
Constructor
Description
`AjpMessage(int packetSize)`
Constructs a new AjpMessage with the specified packet size.

## Method Summary

Modifier and Type
Method
Description
`void`
`appendByte(int val)`
Append a byte (1 byte) to the message.
`void`
`appendByteChunk(ByteChunk bc)`
Write a ByteChunk out at the current write position.
`void`
`appendBytes(byte[] b, int off, int numBytes)`
Copy a chunk of bytes into the packet, starting at the current write position.
`void`
`appendBytes(ByteBuffer b)`
Copy a chunk of bytes into the packet, starting at the current write position.
`void`
`appendBytes(MessageBytes mb)`
Write a MessageBytes out at the current write position.
`void`
`appendInt(int val)`
Add a short integer (2 bytes) to the message.
`void`
`end()`
For a packet to be sent to the web server, finish the process of accumulating data and write the length of the data payload into the header.
`void`
`getBodyBytes(MessageBytes mb)`
Read a non-terminated string of bytes from the packet.
`byte[]`
`getBuffer()`
Return the underlying byte buffer.
`byte`
`getByte()`
Read a single byte from the packet and advance the read position.
`void`
`getBytes(MessageBytes mb)`
Read a null-terminated string of bytes from the packet.
`int`
`getInt()`
Read an integer from packet, and advance the read position past it.
`int`
`getLen()`
Return the current message length.
`int`
`getLongInt()`
Read a 32-bit integer from packet, and advance the read position past it.
`protected static String`
`hex(int x)`
Convert an integer to a two-character hex string.
`protected static String`
`hexLine(byte[] buf, int start, int len)`
Generate a hex dump line for debugging.
`int`
`peekInt()`
Peek at the next integer without advancing the read position.
`int`
`processHeader(boolean toContainer)`
Process the AJP message header, validating the message signature.
`void`
`reset()`
Prepare this packet for accumulating a message from the container to the web server.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### sm

protected static final StringManager sm
The string manager for this package.

### buf

protected final byte[] buf
Fixed size buffer.

### pos

protected int pos
The current read or write position in the buffer.

### len

protected int len
This actually means different things depending on whether the packet is read or write. For read, it's the length of the payload (excluding the header). For write, it's the length of the packet as a whole (counting the header). Oh, well.

## Constructor Details

### AjpMessage

public AjpMessage(int packetSize)
Constructs a new AjpMessage with the specified packet size.

Parameters:
  `packetSize` - The size of the packet buffer

## Method Details

### reset

public void reset()
Prepare this packet for accumulating a message from the container to the web server. Set the write position to just after the header (but leave the length unwritten, because it is as yet unknown).

### end

public void end()
For a packet to be sent to the web server, finish the process of accumulating data and write the length of the data payload into the header.

### getBuffer

public byte[] getBuffer()
Return the underlying byte buffer.

Returns:
  The buffer

### getLen

public int getLen()
Return the current message length.

Returns:
  For read, it's the length of the payload (excluding the header). For write, it's the length of the packet as a whole (counting the header).

### appendInt

public void appendInt(int val)
Add a short integer (2 bytes) to the message.

Parameters:
  `val` - The integer to append

### appendByte

public void appendByte(int val)
Append a byte (1 byte) to the message.

Parameters:
  `val` - The byte value to append

### appendBytes

public void appendBytes(MessageBytes mb)
Write a MessageBytes out at the current write position. A null MessageBytes is encoded as a string with length 0.

Parameters:
  `mb` - The data to write

### appendByteChunk

public void appendByteChunk(ByteChunk bc)
Write a ByteChunk out at the current write position. A null ByteChunk is encoded as a string with length 0.

Parameters:
  `bc` - The data to write

### appendBytes

public void appendBytes(byte[] b, int off, int numBytes)
Copy a chunk of bytes into the packet, starting at the current write position. The chunk of bytes is encoded with the length in two bytes first, then the data itself, and finally a terminating \0 (which is not included in the encoded length).

Parameters:
  `b` - The array from which to copy bytes.
  `off` - The offset into the array at which to start copying
  `numBytes` - The number of bytes to copy.

### appendBytes

public void appendBytes(ByteBuffer b)
Copy a chunk of bytes into the packet, starting at the current write position. The chunk of bytes is encoded with the length in two bytes first, then the data itself, and finally a terminating \0 (which is not included in the encoded length).

Parameters:
  `b` - The ByteBuffer from which to copy bytes.

### getInt

public int getInt()
Read an integer from packet, and advance the read position past it. Integers are encoded as two unsigned bytes in big-endian order (high-order byte first).

Returns:
  The integer value read from the message

### peekInt

public int peekInt()
Peek at the next integer without advancing the read position.

Returns:
  The integer value at the current read position

### getByte

public byte getByte()
Read a single byte from the packet and advance the read position.

Returns:
  The byte value read from the message

### getBytes

public void getBytes(MessageBytes mb)
Read a null-terminated string of bytes from the packet.

Parameters:
  `mb` - The MessageBytes to populate with the read bytes

### getBodyBytes

public void getBodyBytes(MessageBytes mb)
Read a non-terminated string of bytes from the packet.

Parameters:
  `mb` - The MessageBytes to populate with the read bytes

### getLongInt

public int getLongInt()
Read a 32-bit integer from packet, and advance the read position past it. The integer is encoded as four unsigned bytes in big-endian order (high-order byte first).

Returns:
  The integer value read from the message

### processHeader

public int processHeader(boolean toContainer)
Process the AJP message header, validating the message signature.

Parameters:
  `toContainer` - `true` if the message is from the web server to the container, `false` if from the container to the web server
Returns:
  The length of the payload, or -1 if the message signature is invalid

### hexLine

protected static String hexLine(byte[] buf, int start, int len)
Generate a hex dump line for debugging.

Parameters:
  `buf` - The byte buffer
  `start` - The starting offset
  `len` - The current message length
Returns:
  A string representation of the hex dump line

### hex

protected static String hex(int x)
Convert an integer to a two-character hex string.

Parameters:
  `x` - the integer to convert
Returns:
  the hex string