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

📄 mbuffer.java

📁 httptunnel.jar httptunnel java 源码
💻 JAVA
字号:
package net.jumperz.io;

import java.util.*;
import java.io.*;
import net.jumperz.util.MStreamUtil;

public final class MBuffer
extends OutputStream
{
private static Set tmpFileSet = new HashSet();
private static Set streamSet = new HashSet();
private static final int DEFAULT_MAX_MEM_SIZE = 1024 * 1024 * 10; //10MByte
private static final int BUFSIZE = 2048;
private static int staticMaxMemSize = DEFAULT_MAX_MEM_SIZE;

private int totalSize = 0;
private OutputStream bufStream;
private OutputStream fileStream;
private ByteArrayOutputStream byteStream;
private boolean isSmall;
private File tmpFile;
private int maxMemSize;
// --------------------------------------------------------------------------------
public static void setStaticMaxMemSize( int i )
{
staticMaxMemSize = i;
}
//--------------------------------------------------------------------------------
public static void deleteTmpFiles()
{
Iterator p = tmpFileSet.iterator();
while( p.hasNext() )
	{
	File tmpFile = ( File )p.next();
	tmpFile.delete();
	}
}
// --------------------------------------------------------------------------------
public static void closeStreams()
{
Iterator p = streamSet.iterator();
while( p.hasNext() )
	{
	OutputStream s = ( OutputStream )p.next();
	MStreamUtil.closeStream( s );
	}
}
// --------------------------------------------------------------------------------
public MBuffer( int size )
{
maxMemSize = size;
init();
}
//----------------------------------------------------------------------------------
public MBuffer()
{
maxMemSize = staticMaxMemSize;
init();
}
// --------------------------------------------------------------------------------
public void write( int i )
throws IOException
{
bufStream.write( i );
}
//--------------------------------------------------------------------------------
public void close()
{
if( bufStream != null )
	{
	try
		{
		bufStream.close();
		}
	catch( IOException e )
		{
		e.printStackTrace();
		}
	}
}
//----------------------------------------------------------------------------------
private void init()
{
/*
if( bufStream != null )
	{
	try
		{
		bufStream.close();
		}
	catch( IOException e )
		{
		e.printStackTrace();
		}
	}

if( tmpFile != null )
	{
	tmpFile.delete();
	}
*/

tmpFile		= null;
isSmall		= true;
byteStream	= new ByteArrayOutputStream( BUFSIZE );
bufStream	= byteStream;
}
//----------------------------------------------------------------------------------
public void write( byte[] buffer, int len )
throws IOException
{
write( buffer, 0, len );
}
// --------------------------------------------------------------------------------
public int getSize()
{
return totalSize;
}
// --------------------------------------------------------------------------------
public void write( byte[] buffer, int offset, int len )
throws IOException
{
if( isSmall
 && totalSize < maxMemSize
 && ( totalSize + len ) >= maxMemSize
  )
	{
	changeStream();
	}

bufStream.write( buffer, offset, len );
totalSize += len;
}
// --------------------------------------------------------------------------------
public byte[] getBytes()
throws IOException
{
return MStreamUtil.streamToBytes( getInputStream() );
}
//----------------------------------------------------------------------------------
public void write( byte[] buffer )
throws IOException
{
int len = buffer.length;
write( buffer, len );
}
//----------------------------------------------------------------------------------
private void changeStream()
throws IOException
{
isSmall = false;

	// copy data
tmpFile = File.createTempFile( "mbuffer_", ".buf" );
synchronized( MBuffer.tmpFileSet )
	{
	MBuffer.tmpFileSet.add( tmpFile );
	}
tmpFile.deleteOnExit();

fileStream = new FileOutputStream( tmpFile );
synchronized( MBuffer.streamSet )
	{
	MBuffer.streamSet.add( fileStream );
	}
byteStream.writeTo( fileStream );
byteStream = null;

bufStream = fileStream;
}
//----------------------------------------------------------------------------------
public InputStream getInputStream()
{
InputStream inputStream = null;
if( isSmall )
	{
	byte[] buffer = byteStream.toByteArray();
	inputStream = new ByteArrayInputStream( buffer );
	}
else
	{
	try
		{
		inputStream = new FileInputStream( tmpFile ); 
		}
	catch( FileNotFoundException e )
		{
		e.printStackTrace();
		}
	}
return inputStream;
}
//--------------------------------------------------------------------------------
public void deleteTmpFile()
{
if( !isSmall )
	{
	try
		{
		fileStream.close();
		}
	catch( IOException e )
		{
		e.printStackTrace();
		}

	MBuffer.tmpFileSet.remove( tmpFile );
	tmpFile.delete();
	}
}
//----------------------------------------------------------------------------------
}

⌨️ 快捷键说明

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