|
What this is
Other links
The source code
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jk.common;
import java.io.IOException;
import java.io.InputStream;
import org.apache.jk.core.JkHandler;
import org.apache.jk.core.Msg;
import org.apache.jk.core.MsgContext;
import org.apache.tomcat.util.buf.ByteChunk;
/** Generic input stream impl on top of ajp
*/
public class JkInputStream extends InputStream {
private static org.apache.commons.logging.Log log=
org.apache.commons.logging.LogFactory.getLog( JkInputStream.class );
public JkInputStream() {
}
public int available() throws IOException {
if( log.isDebugEnabled() )
log.debug( "available(): " + blen + " " + pos );
return blen-pos;
}
public void close() throws IOException {
if( log.isDebugEnabled() )
log.debug( "cloae() " );
this.closed=true;
}
public void mark(int readLimit) {
}
public boolean markSupported() {
return false;
}
public void reset() throws IOException {
throw new IOException("reset() not supported");
}
public int read() throws IOException {
if( contentLength == -1 ) {
return doRead1();
}
if( available <= 0 ) {
if( log.isDebugEnabled() )
log.debug("doRead() nothing available" );
return -1;
}
available--;
return doRead1();
}
public int read(byte[] b) throws IOException {
int rd=read( b, 0, b.length);
if( log.isDebugEnabled() )
log.debug("read(" + b + ")=" + rd + " / " + b.length);
return rd;
}
public int read(byte[] b, int off, int len) throws IOException {
int rd=-1;
if( contentLength == -1 ) {
rd=doRead1(b,off,len);
return rd;
}
if( available <= 0 ) {
if( log.isDebugEnabled() ) log.debug("doRead() nothing available" );
return -1;
}
rd=doRead1( b,off, len );
available -= rd;
if( log.isDebugEnabled() )
log.debug("Read: " + new String( b,off, len ));
return rd;
}
public long skip(long n) throws IOException {
if (n > Integer.MAX_VALUE) {
throw new IOException("can't skip than many: " + n);
}
// XXX if n is big, split this in multiple reads
byte[] b = new byte[(int)n];
return read(b, 0, b.length);
}
// -------------------- Jk specific methods --------------------
Msg bodyMsg=new MsgAjp();
MsgContext mc;
// Total length of the body - maximum we can read
// If -1, we don't use any limit, and we don't count available
int contentLength;
// How much remains unread.
int available;
boolean closed=false;
// Ajp13 specific - needs refactoring for the new model
public static final int MAX_PACKET_SIZE=8192;
public static final int H_SIZE=4; // Size of basic packet header
public static final int MAX_READ_SIZE = MAX_PACKET_SIZE - H_SIZE - 2;
public static final byte JK_AJP13_GET_BODY_CHUNK = 6;
// Holds incoming chunks of request body data
// XXX We do a copy that could be avoided !
byte []bodyBuff = new byte[9000];
int blen; // Length of current chunk of body data in buffer
int pos; // Current read position within that buffer
boolean end_of_stream=false; // true if we've received an empty packet
private int doRead1() throws IOException {
if(pos >= blen) {
if( ! refillReadBuffer()) {
return -1;
}
}
int i=bodyBuff[pos++] & 0xFF;
if( log.isDebugEnabled() ) log.debug("doRead1 " + (char)i );
return i; // prevent sign extension of byte value
}
public int doRead1(byte[] b, int off, int len) throws IOException
{
if(pos >= blen) {
if( ! refillReadBuffer()) {
return -1;
}
}
if(pos + len <= blen) { // Fear the off by one error
// Sanity check b.length > off + len?
System.arraycopy(bodyBuff, pos, b, off, len);
if( log.isDebugEnabled() )
log.debug("doRead1: " + pos + " " + len + " " + blen);
if( log.isTraceEnabled() )
log.trace("Data: \n" + new String( b, off, len ));
pos += len;
return len;
}
// Not enough data (blen < pos + len) or chunked encoded
int toCopy = len;
while(toCopy > 0) {
int bytesRemaining = blen - pos;
if(bytesRemaining < 0)
bytesRemaining = 0;
int c = bytesRemaining < toCopy ? bytesRemaining : toCopy;
System.arraycopy(bodyBuff, pos, b, off, c);
if( log.isDebugEnabled() )
log.debug("doRead2: " + pos + " " + len + " " +
blen + " " + c);
if( log.isTraceEnabled() )
log.trace("Data: \n" + new String( b, off, (len
|
Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com