📄 mstreamutil.java
字号:
package net.jumperz.util;
import java.io.*;
import java.nio.charset.UnsupportedCharsetException;
import java.net.*;
public final class MStreamUtil
{
private static final int BUFSIZE = 1024;
// --------------------------------------------------------------------------------
public static void writeByteToFile( byte[] buffer, String fileName )
throws IOException
{
OutputStream os = null;
try
{
os = new FileOutputStream( fileName );
os.write( buffer );
}
finally
{
if( os != null )
{
os.close();
}
}
}
//--------------------------------------------------------------------------------------
public static int connectStream( InputStream in, OutputStream out )
throws IOException
{
int totalSize = 0;
int readSize;
byte[] buffer = new byte[ BUFSIZE ];
while( true )
{
readSize = in.read( buffer );
if( readSize <= 0 )
{
break;
}
totalSize += readSize;
out.write( buffer, 0, readSize );
}
return totalSize;
}
//--------------------------------------------------------------------------------------
public static byte[] streamToBytes( InputStream in )
throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream( BUFSIZE );
MStreamUtil.connectStream( in, out );
return out.toByteArray();
}
//--------------------------------------------------------------------------------------
public static String streamToString( InputStream in )
throws IOException
{
String str = null;
try
{
str = new String( MStreamUtil.streamToBytes( in ), MCharset.CS_ISO_8859_1 );
}
catch( UnsupportedEncodingException e )
{
e.printStackTrace();
}
return str;
}
//--------------------------------------------------------------------------------
public static void closeStream( InputStream in )
{
try
{
if( in != null )
{
in.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
//--------------------------------------------------------------------------------
public static void closeStream( OutputStream out )
{
try
{
if( out != null )
{
out.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
// --------------------------------------------------------------------------------
public static InputStream getInputStreamFromString( String s )
{
InputStream in = null;
try
{
in = new ByteArrayInputStream( s.getBytes( MCharset.CS_ISO_8859_1 ) );
}
catch( UnsupportedEncodingException e )
{
}
return in;
}
// --------------------------------------------------------------------------------
public static InputStream getResourceStream( String resource )
throws IOException
{
ClassLoader classLoader = MStreamUtil.class.getClassLoader();
URL url = classLoader.getResource( resource );
return url.openStream();
}
//--------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -