Using Guava CountingOutputStream – 使用Guava CountingOutputStream

最后修改: 2018年 3月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to look at the CountingOutputStream class and how to use it.

在本教程中,我们要看一下CountingOutputStream类以及如何使用它。

The class can be found in popular libraries like Apache Commons or Google Guava. We’re going focus on the implementation in the Guava library.

该类可以在流行的库中找到,如Apache CommonsGoogle Guava。我们将专注于Guava库中的实现。

2. CountingOutputStream

2.CountingOutputStream

2.1. Maven Dependency

2.1.Maven的依赖性

CountingOutputStream is part of Google’s Guava package.

CountingOutputStream是Google的Guava软件包的一部分。

Let’s start by adding the dependency to the pom.xml:

让我们先把依赖关系添加到pom.xml

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

The latest version of the dependency can be checked here.

最新版本的依赖关系可以在这里检查。

2.2. Class Details

2.2.类的详细信息

The class extends java.io.FilterOutputStream, overrides the write() and close() methods, and provides the new method getCount().

该类扩展了java.io.FilterOutputStream,覆盖了write()close()方法,并提供了新方法getCount()

The constructor takes another OutputStream object as an input parameter. While writing data, the class then counts the number of bytes written into this OutputStream.

构造函数将另一个OutputStream对象作为输入参数。在写入数据时,该类会计算写入该OutputStream中的字节数。

In order to get the count, we can simply call getCount() to return the current number of bytes:

为了获得计数,我们可以简单地调用getCount()来返回当前字节数。

/** Returns the number of bytes written. */
public long getCount() {
    return count;
}

3. Use Case

3.使用案例

Let’s use CountingOutputStream in a practical use case. For the sake of example, we’re going to put the code into a JUnit test to make it executable.

让我们在一个实际的用例中使用CountingOutputStream。为了举例说明,我们将把代码放到JUnit测试中,使其可以执行。

In our case, we’re going to write data to an OutputStream and check if we have reached a limit of MAX bytes.

在我们的例子中,我们将把数据写入OutputStream,并检查我们是否达到MAX字节的限制。

Once we reach the limit, we want to break the execution by throwing an exception:

一旦我们达到极限,我们要通过抛出一个异常来中断执行。

public class GuavaCountingOutputStreamUnitTest {
    static int MAX = 5;

    @Test(expected = RuntimeException.class)
    public void givenData_whenCountReachesLimit_thenThrowException()
      throws Exception {
 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        CountingOutputStream cos = new CountingOutputStream(out);

        byte[] data = new byte[1024];
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        
        int b;
        while ((b = in.read()) != -1) {
            cos.write(b);
            if (cos.getCount() >= MAX) {
                throw new RuntimeException("Write limit reached");
            }
        }
    }
}

4. Conclusion

4.结论

In this quick article, we’ve looked at the CountingOutputStream class and its usage. The class provides the additional method getCount() that returns the number of bytes written to the OutputStream so far.

在这篇快速文章中,我们看了CountingOutputStream类和它的用法。该类提供了额外的方法getCount(),用于返回到目前为止写入OutputStream的字节数。

Finally, as always, the code used during the discussion can be found over on GitHub.

最后,像往常一样,讨论中使用的代码可以在GitHub上找到