- 浏览: 1236042 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (242)
- java (58)
- netty (14)
- javascript (21)
- commons (13)
- 读书笔记 (5)
- java测试 (6)
- database (5)
- struts2 (8)
- hibernate (6)
- english (27)
- spring (10)
- 生活 (4)
- 多线程 (4)
- 正则表达式 (1)
- 杂项 (1)
- maven (4)
- 数据库 (10)
- 学习笔记 (1)
- mongodb (1)
- 百度bcs (4)
- 云推送javasdk (2)
- webservice (3)
- IllegalAnnotationException: Two classes have the same XML type name (0)
- drools (3)
- freemarker (3)
- tomcat (1)
- html5 (2)
- mq (11)
- fastjson (3)
- 小算法 (2)
最新评论
-
longxitian:
https://www.cnblogs.com/jeffen/ ...
万恶的Mybatis的EnumTypeHandler -
asialee:
ddnzero 写道博主请问FileUtils这个类是哪个包的 ...
使用mockftpserver进行ftp测试 -
ddnzero:
博主请问FileUtils这个类是哪个包的?还是自己的呢?能放 ...
使用mockftpserver进行ftp测试 -
yizishou:
为什么会intMap.get("bbb") ...
浅谈System.identityHashCode -
liguanqun811:
感觉LogManager打开了所有的LogSegment(文件 ...
jafka学习之LogManager
1. ThresholdingOutputStream
这个类的意图主要是可以处理设置了临界值的OutputStream做出处理,
它当然是复写了三个write方法。
有三个步骤:
1. 检测是否到达临界值
2. 调用底层的Stream写数据
3. 改变已写数据的游标written
里面有两个抽象的方法:
protected abstract OutputStream getStream()
protected abstract void thresholdReached()
意图分别是得到底层的Stream,一个是可以让子类在threshold到达后可以做出响应。
2. DeferredFileOutputStream
是ThresholdingOutputStream的子类
它的意图也很明确,主要是实现延迟写。
首先是数据会写到基于内存的OutStream, 当到达threshold后,会将内存中的数据又
写到指定的文件里面。这个类里面比较有亮点的是thresholdReached函数的实现,
首先将memoryOutputStream里面的数据写到目标文件的FileOutputStream里面,
将memoryOutputStream设置成null,然后将currentOutputStream更新为目标文件的
FileOutputStream,这样后续的数据就会写入文件中。
package org.apache.commons.io.output; import java.io.IOException; import java.io.OutputStream; /** * An output stream which triggers an event when a specified number of bytes of * data have been written to it. The event can be used, for example, to throw * an exception if a maximum has been reached, or to switch the underlying * stream type when the threshold is exceeded. * <p> * This class overrides all <code>OutputStream</code> methods. However, these * overrides ultimately call the corresponding methods in the underlying output * stream implementation. * <p> * NOTE: This implementation may trigger the event <em>before</em> the threshold * is actually reached, since it triggers when a pending write operation would * cause the threshold to be exceeded. * * @author <a href="mailto:martinc@apache.org">Martin Cooper</a> * * @version $Id: ThresholdingOutputStream.java 540714 2007-05-22 19:39:44Z niallp $ */ public abstract class ThresholdingOutputStream extends OutputStream { // ----------------------------------------------------------- Data members /** * The threshold at which the event will be triggered. */ private int threshold; /** * The number of bytes written to the output stream. */ private long written; /** * Whether or not the configured threshold has been exceeded. */ private boolean thresholdExceeded; // ----------------------------------------------------------- Constructors /** * Constructs an instance of this class which will trigger an event at the * specified threshold. * * @param threshold The number of bytes at which to trigger an event. */ public ThresholdingOutputStream(int threshold) { this.threshold = threshold; } // --------------------------------------------------- OutputStream methods /** * Writes the specified byte to this output stream. * * @param b The byte to be written. * * @exception IOException if an error occurs. */ public void write(int b) throws IOException { checkThreshold(1); getStream().write(b); written++; } /** * Writes <code>b.length</code> bytes from the specified byte array to this * output stream. * * @param b The array of bytes to be written. * * @exception IOException if an error occurs. */ public void write(byte b[]) throws IOException { checkThreshold(b.length); getStream().write(b); written += b.length; } /** * Writes <code>len</code> bytes from the specified byte array starting at * offset <code>off</code> to this output stream. * * @param b The byte array from which the data will be written. * @param off The start offset in the byte array. * @param len The number of bytes to write. * * @exception IOException if an error occurs. */ public void write(byte b[], int off, int len) throws IOException { checkThreshold(len); getStream().write(b, off, len); written += len; } /** * Flushes this output stream and forces any buffered output bytes to be * written out. * * @exception IOException if an error occurs. */ public void flush() throws IOException { getStream().flush(); } /** * Closes this output stream and releases any system resources associated * with this stream. * * @exception IOException if an error occurs. */ public void close() throws IOException { try { flush(); } catch (IOException ignored) { // ignore } getStream().close(); } // --------------------------------------------------------- Public methods /** * Returns the threshold, in bytes, at which an event will be triggered. * * @return The threshold point, in bytes. */ public int getThreshold() { return threshold; } /** * Returns the number of bytes that have been written to this output stream. * * @return The number of bytes written. */ public long getByteCount() { return written; } /** * Determines whether or not the configured threshold has been exceeded for * this output stream. * * @return <code>true</code> if the threshold has been reached; * <code>false</code> otherwise. */ public boolean isThresholdExceeded() { return (written > threshold); } // ------------------------------------------------------ Protected methods /** * Checks to see if writing the specified number of bytes would cause the * configured threshold to be exceeded. If so, triggers an event to allow * a concrete implementation to take action on this. * * @param count The number of bytes about to be written to the underlying * output stream. * * @exception IOException if an error occurs. */ protected void checkThreshold(int count) throws IOException { if (!thresholdExceeded && (written + count > threshold)) { thresholdExceeded = true; thresholdReached(); } } /** * Resets the byteCount to zero. You can call this from * {@link #thresholdReached()} if you want the event to be triggered again. */ protected void resetByteCount() { this.thresholdExceeded = false; this.written = 0; } // ------------------------------------------------------- Abstract methods /** * Returns the underlying output stream, to which the corresponding * <code>OutputStream</code> methods in this class will ultimately delegate. * * @return The underlying output stream. * * @exception IOException if an error occurs. */ protected abstract OutputStream getStream() throws IOException; /** * Indicates that the configured threshold has been reached, and that a * subclass should take whatever action necessary on this event. This may * include changing the underlying output stream. * * @exception IOException if an error occurs. */ protected abstract void thresholdReached() throws IOException; }
package org.apache.commons.io.output; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.io.IOUtils; /** * An output stream which will retain data in memory until a specified * threshold is reached, and only then commit it to disk. If the stream is * closed before the threshold is reached, the data will not be written to * disk at all. * <p> * This class originated in FileUpload processing. In this use case, you do * not know in advance the size of the file being uploaded. If the file is small * you want to store it in memory (for speed), but if the file is large you want * to store it to file (to avoid memory issues). * * @author <a href="mailto:martinc@apache.org">Martin Cooper</a> * @author gaxzerow * * @version $Id: DeferredFileOutputStream.java 606381 2007-12-22 02:03:16Z ggregory $ */ public class DeferredFileOutputStream extends ThresholdingOutputStream { // ----------------------------------------------------------- Data members /** * The output stream to which data will be written prior to the theshold * being reached. */ private ByteArrayOutputStream memoryOutputStream; /** * The output stream to which data will be written at any given time. This * will always be one of <code>memoryOutputStream</code> or * <code>diskOutputStream</code>. */ private OutputStream currentOutputStream; /** * The file to which output will be directed if the threshold is exceeded. */ private File outputFile; /** * The temporary file prefix. */ private String prefix; /** * The temporary file suffix. */ private String suffix; /** * The directory to use for temporary files. */ private File directory; /** * True when close() has been called successfully. */ private boolean closed = false; // ----------------------------------------------------------- Constructors /** * Constructs an instance of this class which will trigger an event at the * specified threshold, and save data to a file beyond that point. * * @param threshold The number of bytes at which to trigger an event. * @param outputFile The file to which data is saved beyond the threshold. */ public DeferredFileOutputStream(int threshold, File outputFile) { super(threshold); this.outputFile = outputFile; memoryOutputStream = new ByteArrayOutputStream(); currentOutputStream = memoryOutputStream; } /** * Constructs an instance of this class which will trigger an event at the * specified threshold, and save data to a temporary file beyond that point. * * @param threshold The number of bytes at which to trigger an event. * @param prefix Prefix to use for the temporary file. * @param suffix Suffix to use for the temporary file. * @param directory Temporary file directory. * * @since Commons IO 1.4 */ public DeferredFileOutputStream(int threshold, String prefix, String suffix, File directory) { this(threshold, (File)null); if (prefix == null) { throw new IllegalArgumentException("Temporary file prefix is missing"); } this.prefix = prefix; this.suffix = suffix; this.directory = directory; } // --------------------------------------- ThresholdingOutputStream methods /** * Returns the current output stream. This may be memory based or disk * based, depending on the current state with respect to the threshold. * * @return The underlying output stream. * * @exception IOException if an error occurs. */ protected OutputStream getStream() throws IOException { return currentOutputStream; } /** * Switches the underlying output stream from a memory based stream to one * that is backed by disk. This is the point at which we realise that too * much data is being written to keep in memory, so we elect to switch to * disk-based storage. * * @exception IOException if an error occurs. */ protected void thresholdReached() throws IOException { if (prefix != null) { outputFile = File.createTempFile(prefix, suffix, directory); } FileOutputStream fos = new FileOutputStream(outputFile); memoryOutputStream.writeTo(fos); currentOutputStream = fos; memoryOutputStream = null; } // --------------------------------------------------------- Public methods /** * Determines whether or not the data for this output stream has been * retained in memory. * * @return <code>true</code> if the data is available in memory; * <code>false</code> otherwise. */ public boolean isInMemory() { return (!isThresholdExceeded()); } /** * Returns the data for this output stream as an array of bytes, assuming * that the data has been retained in memory. If the data was written to * disk, this method returns <code>null</code>. * * @return The data for this output stream, or <code>null</code> if no such * data is available. */ public byte[] getData() { if (memoryOutputStream != null) { return memoryOutputStream.toByteArray(); } return null; } /** * Returns either the output file specified in the constructor or * the temporary file created or null. * <p> * If the constructor specifying the file is used then it returns that * same output file, even when threashold has not been reached. * <p> * If constructor specifying a temporary file prefix/suffix is used * then the temporary file created once the threashold is reached is returned * If the threshold was not reached then <code>null</code> is returned. * * @return The file for this output stream, or <code>null</code> if no such * file exists. */ public File getFile() { return outputFile; } /** * Closes underlying output stream, and mark this as closed * * @exception IOException if an error occurs. */ public void close() throws IOException { super.close(); closed = true; } /** * Writes the data from this output stream to the specified output stream, * after it has been closed. * * @param out output stream to write to. * @exception IOException if this stream is not yet closed or an error occurs. */ public void writeTo(OutputStream out) throws IOException { // we may only need to check if this is closed if we are working with a file // but we should force the habit of closing wether we are working with // a file or memory. if (!closed) { throw new IOException("Stream not closed"); } if(isInMemory()) { memoryOutputStream.writeTo(out); } else { FileInputStream fis = new FileInputStream(outputFile); try { IOUtils.copy(fis, out); } finally { IOUtils.closeQuietly(fis); } } } }
发表评论
-
使用commons-net对ftp文件上传下载
2012-08-01 18:38 2567项目中由于要使用到ftp服务,虽然之前对edtFT ... -
commons-io之inputstream学习
2010-08-20 19:41 2583ProxyInputStream类的学习 ... -
commons-io之WildcardFileFilter的实现
2010-08-02 11:35 2478上次这个是最后一个FileFilter,没 ... -
commons-io之filefilter学习
2010-07-27 10:23 50831. IOFileFilter接口 这个接口就是 ... -
common-io之Comparator阅读
2010-07-17 00:17 9941. 首先我们来查看AbstractFileCompa ... -
common-io 之ByteArrayOutputStream阅读
2010-07-16 18:56 3113首先我们来分析它里面的的实例变量:buffers: 可以看成是 ... -
一款文件上传信息即时同步刷新的代码的学习
2010-07-06 18:04 1293对下面链接提供的文件上传的代码的学习。 http://mao ... -
FileUpload之FileItem
2010-07-05 18:17 22704FileItem类主要是封装了一个File Item ... -
Digester 1.1 单元测试之RuleTestCase
2010-02-03 23:32 1331前面还记得有前辈说过看一个开源项目在不懂的情况下要去 ... -
Digester 1.1 源码阅读
2010-02-03 20:36 1506前一段时间我们阅读了Digester的最初始的版本1 ... -
Digester 1.0 源码阅读
2010-02-01 20:15 1595近来在学习tomcat ... -
Digester学习
2010-01-26 00:45 1902近来在学习tomcat的源码,其中有个解析XML的 ...
相关推荐
赠送jar包:commons-io-2.2.jar; 赠送原API文档:commons-io-2.2-javadoc.jar; 赠送源代码:commons-io-2.2-sources.jar; 包含翻译后的API文档:commons-io-2.2-javadoc-API文档-中文(简体)版.zip 对应Maven...
赠送jar包:commons-io-2.8.0.jar; 赠送原API文档:commons-io-2.8.0-javadoc.jar; 赠送源代码:commons-io-2.8.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io...
赠送jar包:commons-io-2.11.0.jar; 赠送原API文档:commons-io-2.11.0-javadoc.jar; 赠送源代码:commons-io-2.11.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:...
开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2...
赠送jar包:commons-io-2.5.jar; 赠送原API文档:commons-io-2.5-javadoc.jar; 赠送源代码:commons-io-2.5-sources.jar; 赠送Maven依赖信息文件:commons-io-2.5.pom; 包含翻译后的API文档:commons-io-2.5-...
赠送jar包:commons-io-2.7.jar; 赠送原API文档:commons-io-2.7-javadoc.jar; 赠送源代码:commons-io-2.7-sources.jar; 赠送Maven依赖信息文件:commons-io-2.7.pom; 包含翻译后的API文档:commons-io-2.7-...
赠送jar包:commons-io-1.3.2.jar; 赠送原API文档:commons-io-1.3.2-javadoc.jar; 赠送源代码:commons-io-1.3.2-sources.jar; 赠送Maven依赖信息文件:commons-io-1.3.2.pom; 包含翻译后的API文档:commons-io...
赠送jar包:commons-io-2.7.jar 赠送原API文档:commons-io-2.7-javadoc.jar 赠送源代码:commons-io-2.7-sources.jar 包含翻译后的API文档:commons-io-2.7-javadoc-API文档-中文(简体)-英语-对照版.zip 对应...
在Java开发中,上传文件是一项常见的任务,而`commons-fileupload-1.3.3.jar`和`commons-io-2.6.jar`是Apache Commons项目中的两个重要库,专门用于处理HTTP请求中的文件上传功能。这两个库为开发者提供了便捷、高效...
在实际开发中,当你需要在Web应用中实现文件上传功能时,首先会配置`commons-fileupload-1.2.1.jar`来解析请求,然后利用`commons-io-1.4.jar`处理上传后的文件,比如保存到服务器的指定目录,或者进行一些预处理...
然后在Controller中,可以直接通过`@RequestParam("file") MultipartFile file`来接收上传的文件,这里MultipartFile就是由`commons-fileupload`和`commons-io`支持的。 对于MyBatis,虽然它主要关注数据库操作,但...
在本案例中,我们讨论的是"commons-io-2.4"版本,这个版本包含了完整的Apache Commons IO库的jar包和源代码,适用于JDK 1.6及以上版本。 Apache Commons IO 提供了许多实用工具类,使得开发者在处理输入/输出操作时...
在压缩包中,除了`commons-io-2.4.jar`本身,还有其他URL文件,可能是提供额外资源的链接,如"去脚本之家看看.url"可能指向一个编程资源网站,"领取天猫淘宝内部优惠券.url"可能是促销信息,而"服务器软件.url"可能...
赠送jar包:commons-io-2.8.0.jar; 赠送原API文档:commons-io-2.8.0-javadoc.jar; 赠送源代码:commons-io-2.8.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io...
Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,包含了最新的commons-io-2.0.1-bin,commons-io-2.0.1-src,commons-io-2.0.1-doc
在这个场景中,`commons-fileupload-1.3.1.jar` 和 `commons-io-2.4.jar` 是两个至关重要的库,它们提供了强大的文件上传支持。 `commons-fileupload-1.3.1.jar` 是Apache Commons FileUpload项目的实现,这是一个...
赠送jar包:commons-io-2.11.0.jar; 赠送原API文档:commons-io-2.11.0-javadoc.jar; 赠送源代码:commons-io-2.11.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:...
java-IO流 commons-io-2.11.0.jar
`commons-io-2.5.jar`则是Apache Commons IO库的一部分,它提供了大量的IO操作工具类,这些工具在处理文件、流、过滤器和转换时非常有用。2.5版本同样包含了众多改进和新特性,以适应不断变化的开发需求。 Commons ...
以上只是 `commons-io-2.6.jar` 中部分关键功能的概述,实际使用中,根据具体需求,可以灵活组合这些工具类和方法,以满足各种 I/O 相关的需求。Apache Commons IO 以其强大的功能和良好的兼容性,广泛应用于各种 ...