This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b81e1fe1a60c91f224bba4cf13b649bb0bb2f786 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Fri Jan 17 10:10:21 2020 +0700 JAMES-2997 Provide an efficient 'size' input stream --- .../org/apache/james/util/io/SizeInputStream.java | 101 +++++++++++++++++++ .../apache/james/util/io/SizeInputStreamTest.java | 111 +++++++++++++++++++++ 2 files changed, 212 insertions(+) diff --git a/server/container/util/src/main/java/org/apache/james/util/io/SizeInputStream.java b/server/container/util/src/main/java/org/apache/james/util/io/SizeInputStream.java new file mode 100644 index 0000000..f289e31 --- /dev/null +++ b/server/container/util/src/main/java/org/apache/james/util/io/SizeInputStream.java @@ -0,0 +1,101 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util.io; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.lang3.NotImplementedException; + +public class SizeInputStream extends InputStream { + private final InputStream wrapped; + private long size; + + public SizeInputStream(InputStream wrapped) { + this.wrapped = wrapped; + this.size = 0L; + } + + @Override + public int read() throws IOException { + int read = wrapped.read(); + + if (read > 0) { + size++; + } + + return read; + } + + @Override + public int read(byte[] b) throws IOException { + int read = wrapped.read(b); + return increaseSize(read); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int read = wrapped.read(b, off, len); + return increaseSize(read); + } + + @Override + public long skip(long n) throws IOException { + long skipped = wrapped.skip(n); + return increaseSize(skipped); + } + + @Override + public int available() throws IOException { + return wrapped.available(); + } + + @Override + public void close() throws IOException { + wrapped.close(); + } + + @Override + public void mark(int readlimit) { + throw new NotImplementedException("'mark' is not supported'"); + } + + @Override + public void reset() { + throw new NotImplementedException("'reset' is not supported'"); + } + + @Override + public boolean markSupported() { + return false; + } + + public long getSize() { + return size; + } + + private <T extends Number> T increaseSize(T chunkSize) { + long longValue = chunkSize.longValue(); + if (longValue > 0) { + size += longValue; + } + return chunkSize; + } +} diff --git a/server/container/util/src/test/java/org/apache/james/util/io/SizeInputStreamTest.java b/server/container/util/src/test/java/org/apache/james/util/io/SizeInputStreamTest.java new file mode 100644 index 0000000..ece2d91 --- /dev/null +++ b/server/container/util/src/test/java/org/apache/james/util/io/SizeInputStreamTest.java @@ -0,0 +1,111 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util.io; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +import com.github.fge.lambdas.Throwing; +import com.google.common.base.Strings; + +class SizeInputStreamTest { + static final byte[] BYTES = "0123456789".getBytes(StandardCharsets.UTF_8); + static final byte[] TWELVE_MEGABYTES = Strings.repeat("0123456789\r\n", 1024 * 1024).getBytes(StandardCharsets.UTF_8); + + @Test + void sizeInputStreamShouldNotAlterContent() { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + assertThat(sizeInputStream).hasSameContentAs(new ByteArrayInputStream(BYTES)); + } + + @Test + void sizeInputStreamShouldNotAlterContentOfEmptyStream() { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(new byte[0])); + + assertThat(sizeInputStream).hasSameContentAs(new ByteArrayInputStream(new byte[0])); + } + + @Test + void sizeInputStreamShouldNotAlterContentOfBigStream() { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(TWELVE_MEGABYTES)); + + assertThat(sizeInputStream).hasSameContentAs(new ByteArrayInputStream(TWELVE_MEGABYTES)); + } + + @Test + void getSizeShouldReturnZeroWhenEmpty() { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(new byte[0])); + + assertThat(sizeInputStream.getSize()).isEqualTo(0); + } + + @Test + void getSizeShouldReturnSizeWhenReadWithABiggerBuffer() throws Exception { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + sizeInputStream.read(new byte[24]); + + assertThat(sizeInputStream.getSize()).isEqualTo(10); + } + + @Test + void getSizeShouldReturnSizeWhenReadWithABufferHavingSameSizeThanContent() throws Exception { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + sizeInputStream.read(new byte[10]); + + assertThat(sizeInputStream.getSize()).isEqualTo(10); + } + + @Test + void getSizeShouldReturnSizeWhenReadUsingSmallerBuffers() throws Exception { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + sizeInputStream.read(new byte[6]); + sizeInputStream.read(new byte[6]); + + assertThat(sizeInputStream.getSize()).isEqualTo(10); + } + + @Test + void getSizeShouldReturnSizeWhenReadByte() { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + IntStream.range(0, 10).forEach(Throwing.intConsumer(step -> sizeInputStream.read())); + + assertThat(sizeInputStream.getSize()).isEqualTo(10); + } + + @Test + void getSizeShouldReturnSizeWhenSkips() throws Exception { + SizeInputStream sizeInputStream = new SizeInputStream(new ByteArrayInputStream(BYTES)); + + sizeInputStream.read(new byte[6]); + sizeInputStream.skip(6); + + assertThat(sizeInputStream.getSize()).isEqualTo(10); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org