本文整理了Java中org.codehaus.plexus.util.IOUtil
类的一些代码示例,展示了IOUtil
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtil
类的具体详情如下:
包路径:org.codehaus.plexus.util.IOUtil
类名称:IOUtil
[英]General IO Stream manipulation.
This class provides static utility methods for input/output operations, particularly buffered copying between sources (InputStream
, Reader
, String
and byte[]
) and destinations (OutputStream
, Writer
, String
and byte[]
).
Unless otherwise noted, these copy
methods do not flush or close the streams. Often, doing so would require making non-portable assumptions about the streams' origin and further use. This means that both streams' close()
methods must be called after copying. if one omits this step, then the stream resources (sockets, file descriptors) are released when the associated Stream is garbage-collected. It is not a good idea to rely on this mechanism. For a good overview of the distinction between "memory management" and "resource management", see this UnixReview article
For each copy
method, a variant is provided that allows the caller to specify the buffer size (the default is 4k). As the buffer size can have a fairly large impact on speed, this may be worth tweaking. Often "large buffer -> faster" does not hold, even for large data transfers.
For byte-to-char methods, a copy
variant allows the encoding to be selected (otherwise the platform default is used).
The copy
methods use an internal buffer when copying. It is therefore advisable not to deliberately wrap the stream arguments to the copy
methods in Buffered*
streams. For example, don't do the following:copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );
The rationale is as follows:
Imagine that an InputStream's read() is a very expensive operation, which would usually suggest wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent java.io.InputStream#read(byte[] b,int off,int len) requests on the underlying InputStream, to fill an internal buffer, from which further read
requests can inexpensively get their data (until the buffer runs out).
However, the copy
methods do the same thing, keeping an internal buffer, populated by InputStream#read(byte[] b,int off,int len) requests. Having two buffers (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer management hurts performance slightly (about 3%, according to some simple experiments).
[中]通用IO流操作。
此类为输入/输出操作提供静态实用程序方法,特别是源(InputStream
、Reader
、String
和byte[]
与目标(OutputStream
、Writer
、String
和byte[]
之间的缓冲复制。
除非另有说明,这些copy
方法不会刷新或关闭流。通常,这样做需要对流的起源和进一步使用做出不可移植的假设。这意味着复制后必须调用两个流的close()
方法。如果省略此步骤,则在对关联流进行垃圾收集时释放流资源(套接字、文件描述符)。依靠这种机制不是一个好主意。有关“内存管理”和“资源管理”之间区别的详细概述,请参见this UnixReview article
对于每个copy
方法,都提供了一个变量,允许调用方指定缓冲区大小(默认值为4k)。由于缓冲区大小会对速度产生相当大的影响,这可能值得调整。通常,“大缓冲->更快”不起作用,即使对于大数据传输也是如此。
对于字节到字符的方法,copy
变量允许选择编码(否则使用平台默认值)。copy
方法在复制时使用内部缓冲区。因此,建议不要故意将流参数包装到Buffered*
流中的copy
方法。例如,不要执行以下操作:copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );
理由如下:
假设InputStream的read()是一个非常昂贵的操作,通常建议将其包装到BufferedInputStream中。BufferedInputStream通过发出不频繁的java来工作。木卫一。InputStream#读取底层InputStream上的(字节[]b,int off,int len)请求,以填充内部缓冲区,进一步的read
请求可以从中廉价地获取数据(直到缓冲区用完)。
但是,copy
方法做同样的事情,保留一个内部缓冲区,由InputStream#read(字节[]b,int off,int len)请求填充。拥有两个缓冲区(或者如果目标流也被缓冲,则有三个缓冲区)是没有意义的,而且不必要的缓冲区管理对性能有轻微的影响(根据一些简单的实验,大约3%)。
代码示例来源:origin: org.codehaus.plexus/plexus-utils
try
OutputStream out = new FileOutputStream( file );
if ( encoding != null )
writer = new OutputStreamWriter( out, encoding );
writer = new OutputStreamWriter( out );
IOUtil.close( writer );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
output = new FileOutputStream( destination );
IOUtil.copy( input, output );
output.close();
output = null;
input.close();
input = null;
IOUtil.close( input );
IOUtil.close( output );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
/**
* Get the contents of an <code>InputStream</code> as a String. The platform's default encoding is used for the
* byte-to-char conversion.
*/
public static String toString( final InputStream input )
throws IOException
{
return toString( input, DEFAULT_BUFFER_SIZE );
}
代码示例来源:origin: org.codehaus.plexus/plexus-utils
try
input1 = new FileInputStream( file1 );
input2 = new FileInputStream( file2 );
equals = IOUtil.contentEquals( input1, input2 );
input1.close();
input1 = null;
input2.close();
input2 = null;
IOUtil.close( input1 );
IOUtil.close( input2 );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
FileInputStream instream = new FileInputStream( from );
FileOutputStream outstream = new FileOutputStream( to );
fileWriter = new OutputStreamWriter( outstream, encoding );
IOUtil.copy( reader, fileWriter );
fileWriter.close();
fileWriter = null;
IOUtil.close( fileReader );
IOUtil.close( fileWriter );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
try
fis = new FileInputStream( source );
fos = new FileOutputStream( destination );
input = fis.getChannel();
output = fos.getChannel();
long size = input.size();
long pos = 0;
fos.close();
fos = null;
input.close();
input = null;
fis.close();
fis = null;
IOUtil.close( output );
IOUtil.close( fos );
IOUtil.close( input );
IOUtil.close( fis );
代码示例来源:origin: org.apache.maven.plugins/maven-eclipse-plugin
/**
* @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
*/
public void write()
throws MojoExecutionException
{
// create a .settings directory (if not existing)
File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
settingsDir.mkdirs();
Writer w;
String packaging = config.getPackaging();
// Write out facet core xml
try
{
w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
}
catch ( IOException ex )
{
throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
}
XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
writeModuleTypeFacetCore( writer, packaging );
IOUtil.close( w );
}
代码示例来源:origin: org.codehaus.plexus/plexus-utils
try
out = new FileOutputStream( fileName, true );
if ( encoding != null )
out.write( data.getBytes( encoding ) );
out.write( data.getBytes() );
out.close();
IOUtil.close( out );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
reader = new InputStreamReader( new FileInputStream( file ), encoding );
reader = new InputStreamReader( new FileInputStream( file ) );
IOUtil.close( reader );
代码示例来源:origin: org.mule.tools.maven/mule-app-maven-plugin
private void copyZipFileToDestDirUsingTempFile(File zipFile, File dest) throws IOException, MojoExecutionException
{
if (!dest.isDirectory())
{
throw new IllegalArgumentException("destination must be a directory");
}
InputStream muleZipInput = null;
OutputStream tempOutput = null;
try
{
muleZipInput = new FileInputStream(zipFile);
File tempFile = new File(dest, zipFile.getName().replace(".zip", ".temp"));
tempOutput = new FileOutputStream(tempFile);
IOUtil.copy(muleZipInput, tempOutput);
getLog().info(String.format("Copying %1s to %2s", zipFile.getAbsolutePath(),
tempFile.getAbsolutePath()));
File finalFile = new File(dest, zipFile.getName());
if (tempFile.renameTo(finalFile) == false)
{
throw new MojoExecutionException(String.format("Could not rename %1s to %2s",
tempFile.getAbsolutePath(), finalFile.getAbsolutePath()));
}
}
finally
{
IOUtil.close(muleZipInput);
IOUtil.close(tempOutput);
}
}
代码示例来源:origin: org.codehaus.plexus/plexus-archiver
private Manifest getManifest( File manifestFile )
throws ArchiverException
{
InputStream in = null;
try
{
in = new FileInputStream( manifestFile );
final Manifest mf = getManifest( in );
in.close();
in = null;
return mf;
}
catch ( IOException e )
{
throw new ArchiverException( "Unable to read manifest file: " + manifestFile + " (" + e.getMessage() + ")",
e );
}
finally
{
IOUtil.close( in );
}
}
代码示例来源:origin: org.codehaus.mojo/wagon-maven-plugin
private String checksum( File file, String type )
throws IOException, NoSuchAlgorithmException
{
MessageDigest md5 = MessageDigest.getInstance( type );
InputStream is = new FileInputStream( file );
byte[] buf = new byte[8192];
int i;
while ( ( i = is.read( buf ) ) > 0 )
{
md5.update( buf, 0, i );
}
IOUtil.close( is );
return encode( md5.digest() );
}
代码示例来源:origin: org.codehaus.plexus/plexus-archiver
count = zIn.read( buffer, 0, buffer.length );
zIn.close();
zIn = null;
IOUtil.close( zIn );
代码示例来源:origin: org.codehaus.plexus/plexus-utils
public static Properties loadProperties( final InputStream is )
throws IOException
{
InputStream in = is;
try
{
final Properties properties = new Properties();
// Make sure the properties stream is valid
if ( in != null )
{
properties.load( in );
in.close();
in = null;
}
return properties;
}
finally
{
IOUtil.close( in );
}
}
代码示例来源:origin: org.apache.maven.plugins/maven-install-plugin
public void calculate( File file )
throws MojoExecutionException
{
FileInputStream fis = null;
BufferedInputStream bis = null;
try
{
fis = new FileInputStream( file );
calculate( fis );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Failed to calculate digest checksum for " + file, e );
}
finally
{
IOUtil.close( bis );
IOUtil.close( fis );
}
}
代码示例来源:origin: org.apache.npanday.plugins/maven-vsinstaller-plugin
private void writePlugin( File addinPath )
throws MojoExecutionException
{
OutputStreamWriter writer = null;
if ( !addinPath.exists() )
{
addinPath.mkdirs();
}
try
{
String addin = IOUtil.toString( VsInstallerMojo.class.getResourceAsStream(
"/template/NPanday.VisualStudio.AddIn" ) );
File outputFile = new File( addinPath, "NPanday.VisualStudio.AddIn" );
writer = new OutputStreamWriter( new FileOutputStream( outputFile ), "Unicode" );
writer.write( addin.replaceAll( "\\$\\{installationLocation\\}", installationLocation.getAbsolutePath().replaceAll( "\\\\", "\\\\\\\\" ) ) );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Unable to write to Visual Studio AddIns directory: " + e.getMessage() );
}
finally
{
IOUtil.close( writer );
}
}
代码示例来源:origin: org.apache.maven.plugins/maven-eclipse-plugin
private static void storeProperties( Properties props, File f )
throws MojoExecutionException
{
OutputStream os = null;
try
{
os = new FileOutputStream( f );
props.store( os, null );
}
catch ( IOException ioe )
{
throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", //$NON-NLS-1$
f.getAbsolutePath() ) );
}
finally
{
IOUtil.close( os );
}
}
}
代码示例来源:origin: takari/polyglot-maven
public void write(final File file, final Map<String, Object> options, final Model model) throws IOException {
assert file != null;
assert model != null;
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
write(out, options, model);
out.flush();
} finally {
IOUtil.close(out);
}
}
代码示例来源:origin: simpligility/android-maven-plugin
try
final OutputStream out = new FileOutputStream( entryFile );
try
IOUtil.copy( in, out );
代码示例来源:origin: electronicarts/ea-async
if (!contentDirectory.exists())
getLog().warn(getType() + " directory is empty! " + contentDirectory);
return;
transformer.setErrorListener(error -> getLog().error(error));
int instrumentedCount = 0;
ClassLoader classLoader = createClassLoader();
getLog().error("Error instrumenting " + resource.getName(), e);
getLog().debug("instrumented: " + resource.getName());
IOUtil.copy(bytes, new FileOutputStream(new File(getOutputDirectory(), resource.getName())));
instrumentedCount++;
throw new MojoExecutionException("Error assembling instrumenting", e);
内容来源于网络,如有侵权,请联系作者删除!