⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mhttpdata.java

📁 httptunnel.jar httptunnel java 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}

int contentLength = 0;
MBuffer dummyBuffer = new MBuffer();
MBuffer newBuffer = new MBuffer();

BufferedInputStream bis = new BufferedInputStream( getBodyInputStream() );

	// mark
bis.mark( Integer.MAX_VALUE );

	// read chunk-size, chunk-extension (if any) and CRLF
int chunkSize = readChunkSize( bis, dummyBuffer );

	// while (chunk-size > 0)
while( chunkSize > 0 )
	{
		// read chunk-data
	int received;
	int remain	= chunkSize;
	int canRead	= BODY_BUFSIZE;
	byte[] buffer	= new byte[ BODY_BUFSIZE ];

	while( remain > 0 )
		{	
		if( remain < BODY_BUFSIZE )
			{
			canRead = remain;
			}

		received = bis.read( buffer, 0, canRead );
		if( received <= 0 )
			{
			throw new IOException( "Failed to read the chunked data" );
			}
		remain	-= received;
		
			// append chunk-data to entity-body
		newBuffer.write( buffer, received );
		contentLength += received;
		}

		// and CRLF
	readCRLF( bis, dummyBuffer );

		// reset, skip and mark
	bis.reset();
	bis.skip( chunkSize + 2 );
	bis.mark( Integer.MAX_VALUE );
	
		// read chunk-size and CRLF
	chunkSize = readChunkSize( bis, dummyBuffer );
	}
	
	// read entity-header
String trailer = readTrailer( bis, dummyBuffer );

	// while (entity-header not empty)
while( !trailer.equals( "" ) )
	{	
		// read entity-header
	trailer = readTrailer( bis, dummyBuffer );
	}

bodyBuffer.close();
dummyBuffer.close();
bodyBuffer = newBuffer;

removeHeaderValue( "Transfer-Encoding" );
setHeaderValue( "Content-Length", Integer.toString( contentLength ) );
}
//-------------------------------------------------------------------------------------------
protected final void recvChunkedBody()
throws IOException
{
	// http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.4.6

bodyBuffer = new MBuffer();
	
	// mark
bufferedInputStream.mark( Integer.MAX_VALUE );

	// read chunk-size, chunk-extension (if any) and CRLF
int chunkSize		= readChunkSize( bufferedInputStream, bodyBuffer );

	// while (chunk-size > 0)
while( chunkSize > 0 )
	{
		// read chunk-data
	int received;
	int remain	= chunkSize;
	int canRead	= BODY_BUFSIZE;
	byte[] buffer	= new byte[ BODY_BUFSIZE ];

	while( remain > 0 )
		{	
		if( remain < BODY_BUFSIZE )
			{
			canRead = remain;
			}

		received = bufferedInputStream.read( buffer, 0, canRead );
		if( received <= 0 )
			{
			throw new IOException( "Failed to read the chunked data" );
			}
		remain	-= received;
		
			// append chunk-data to entity-body
		bodyBuffer.write( buffer, received );
		}

		// and CRLF
	readCRLF( bufferedInputStream, bodyBuffer );

		// reset, skip and mark
	bufferedInputStream.reset();
	bufferedInputStream.skip( chunkSize + 2 );
	bufferedInputStream.mark( Integer.MAX_VALUE );
	
		// read chunk-size and CRLF
	chunkSize		= readChunkSize( bufferedInputStream, bodyBuffer );
	}
	
	// read entity-header
String trailer = readTrailer( bufferedInputStream, bodyBuffer );

	// while (entity-header not empty)
while( !trailer.equals( "" ) )
	{	
		// read entity-header
	trailer = readTrailer( bufferedInputStream, bodyBuffer );
	}
}
//-------------------------------------------------------------------------------------------
/*
 * Caution: inputStream must reset, skipped and marked
 */
private final String readTrailer( BufferedInputStream inputStream, MBuffer bodyBuffer )
throws IOException
{
MLineReader reader = getLineReader();
reader.setInputStream( inputStream );

String trailer = reader.readLine();
if( trailer == null )
	{
	return "";
	}

byte[] buffer = trailer.getBytes( MCharset.CS_ISO_8859_1 ); 
bodyBuffer.write( buffer );
bodyBuffer.write( CRLF );

	// reset and skip
inputStream.reset();
inputStream.skip( trailer.length() + 2 );
inputStream.mark( Integer.MAX_VALUE );

return trailer;
}
//-------------------------------------------------------------------------------------------
/*
 * Caution: inputStream must reset, skipped and marked
 */
private final int readChunkSize( BufferedInputStream inputStream, MBuffer bodyBuffer )
throws IOException
{	
MLineReader reader = getLineReader();
reader.setInputStream( inputStream );

String line		= reader.readLine();
if( line == null )
	{
	return 0;
	}

byte[] buffer = line.getBytes( MCharset.CS_ISO_8859_1 );
bodyBuffer.write( buffer );
bodyBuffer.write( CRLF );
String chunkSizeStr	= MRegEx.getMatch( "^[0-9A-Fa-f]{1,}", line );

	// reset, skip and mark
inputStream.reset();
inputStream.skip( line.length() + 2 );
inputStream.mark( Integer.MAX_VALUE );

return Integer.parseInt( chunkSizeStr, 16 );
}
//-------------------------------------------------------------------------------------------
private final void readCRLF( InputStream inputStream, MBuffer bodyBuffer )
throws IOException
{
int CR = inputStream.read();
if( CR != 0x0D )
	{
	throw new IOException( "Invalid CR" );
	}
int LF = inputStream.read();
if( LF != 0x0A )
	{
	throw new IOException( "Invalid LF" );
	}
bodyBuffer.write( CRLF );
}
//-------------------------------------------------------------------------------------------
public final InputStream getBodyInputStream()
{
return bodyBuffer.getInputStream();
}
// --------------------------------------------------------------------------------
public final byte[] getBodyAsByte()
throws IOException
{
return MStreamUtil.streamToBytes( getBodyInputStream() );
}
// --------------------------------------------------------------------------------
public final String getBodyAsString()
throws IOException
{
return MStreamUtil.streamToString( getBodyInputStream() ); 
}
//-------------------------------------------------------------------------------------------
public final synchronized ArrayList getHeaderFieldList()
{
int count = headerList.size();
ArrayList headerFieldList = new ArrayList( count );
for( int i = 0; i < count; ++i )
	{
	String header = ( String )headerList.get( i );
	headerFieldList.add( MRegEx.getMatch( "([^:]+):", header ) );
	}
return headerFieldList;
}
//-------------------------------------------------------------------------------------------
public final List getHeaderList()
{
return new ArrayList( headerList );
}
//--------------------------------------------------------------------------------
public final void setHeaderList( List list )
{
headerList = list;
}
//-------------------------------------------------------------------------------
public final boolean hasBody()
{
return hasBodyFlag;
}
//-------------------------------------------------------------------------------------------
public final void deleteTmpFile()
{
if( bodyBuffer != null )
	{
	bodyBuffer.deleteTmpFile();
	}
}
// --------------------------------------------------------------------------------
private void resetBuffer()
throws IOException
{
if( bodyBuffer != null )
	{
	bodyBuffer.close();
	}
}
// --------------------------------------------------------------------------------
public final void setBody( MBuffer buffer )
throws IOException
{
resetBuffer();

bodyBuffer = buffer;
hasBodyFlag = true;
}
//--------------------------------------------------------------------------------
public final void setBody( byte[] data )
throws IOException
{
resetBuffer();

bodyBuffer = new MBuffer();
bodyBuffer.write( data );
hasBodyFlag = true;
}
// --------------------------------------------------------------------------------
public final void setBody( String str, String charset )
throws IOException
{
setBody( str.getBytes( charset ) );
}
//--------------------------------------------------------------------------------
public final void setBody( String str )
throws IOException
{
setBody( str, MCharset.CS_ISO_8859_1 );
}
// --------------------------------------------------------------------------------
public byte[] toByteArray()
{
ByteArrayOutputStream baos = toByteArrayOutputStream();
return baos.toByteArray();
}
// --------------------------------------------------------------------------------
private ByteArrayOutputStream toByteArrayOutputStream()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream( 512 );
try
	{
	baos.write( getHeader() );
	if( hasBody() )
		{
		MStreamUtil.connectStream( getBodyInputStream(), baos );
		}
	}
catch( IOException e )
	{
	e.printStackTrace();
	}
return baos;
}
//--------------------------------------------------------------------------------
public String toString( String enc )
{
ByteArrayOutputStream baos = toByteArrayOutputStream();
String s = null;
try
	{
	s = baos.toString( enc );
	}
catch( UnsupportedEncodingException e )
	{
	e.printStackTrace();
	}
return s;
}
// --------------------------------------------------------------------------------
public String toString()
{
return toString( MCharset.CS_ISO_8859_1 );
}
//--------------------------------------------------------------------------------
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -