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 a5ea96dc51d2bc4b5f4421045048002c4720702c Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Wed Feb 5 11:46:28 2020 +0700 JAMES-2990 s/SizeInputStream/CurrentPositionInputStream/ --- .../cassandra/mail/CassandraAttachmentMapper.java | 11 +- ...Stream.java => CurrentPositionInputStream.java} | 16 +-- .../util/io/CurrentPositionInputStreamTest.java | 120 +++++++++++++++++++++ .../apache/james/util/io/SizeInputStreamTest.java | 111 ------------------- 4 files changed, 133 insertions(+), 125 deletions(-) diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java index 84e6022..62ed3f0 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java @@ -40,7 +40,7 @@ import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.model.ParsedAttachment; import org.apache.james.mailbox.store.mail.AttachmentMapper; import org.apache.james.util.ReactorUtils; -import org.apache.james.util.io.SizeInputStream; +import org.apache.james.util.io.CurrentPositionInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -112,17 +112,16 @@ public class CassandraAttachmentMapper implements AttachmentMapper { @Override public Mono<Attachment> storeAttachmentForOwner(String contentType, InputStream inputStream, Username owner) { - SizeInputStream sizeInputStream = new SizeInputStream(inputStream); + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(inputStream); AttachmentId attachmentId = AttachmentId.random(); - return ownerDAO.addOwner(attachmentId, owner) - .flatMap(any -> Mono.from(blobStore.save(blobStore.getDefaultBucketName(), sizeInputStream, LOW_COST))) - .map(blobId -> new DAOAttachment(attachmentId, blobId, contentType, sizeInputStream.getSize())) + .flatMap(any -> Mono.from(blobStore.save(blobStore.getDefaultBucketName(), currentPositionInputStream, LOW_COST))) + .map(blobId -> new DAOAttachment(attachmentId, blobId, contentType, currentPositionInputStream.getPosition())) .flatMap(attachmentDAOV2::storeAttachment) .map(any -> Attachment.builder() .attachmentId(attachmentId) .type(contentType) - .size(sizeInputStream.getSize()) + .size(currentPositionInputStream.getPosition()) .build()); } 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/CurrentPositionInputStream.java similarity index 90% rename from server/container/util/src/main/java/org/apache/james/util/io/SizeInputStream.java rename to server/container/util/src/main/java/org/apache/james/util/io/CurrentPositionInputStream.java index f289e31..a739e9d 100644 --- 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/CurrentPositionInputStream.java @@ -24,13 +24,13 @@ import java.io.InputStream; import org.apache.commons.lang3.NotImplementedException; -public class SizeInputStream extends InputStream { +public class CurrentPositionInputStream extends InputStream { private final InputStream wrapped; - private long size; + private long position; - public SizeInputStream(InputStream wrapped) { + public CurrentPositionInputStream(InputStream wrapped) { this.wrapped = wrapped; - this.size = 0L; + this.position = 0L; } @Override @@ -38,7 +38,7 @@ public class SizeInputStream extends InputStream { int read = wrapped.read(); if (read > 0) { - size++; + position++; } return read; @@ -87,14 +87,14 @@ public class SizeInputStream extends InputStream { return false; } - public long getSize() { - return size; + public long getPosition() { + return position; } private <T extends Number> T increaseSize(T chunkSize) { long longValue = chunkSize.longValue(); if (longValue > 0) { - size += longValue; + position += longValue; } return chunkSize; } diff --git a/server/container/util/src/test/java/org/apache/james/util/io/CurrentPositionInputStreamTest.java b/server/container/util/src/test/java/org/apache/james/util/io/CurrentPositionInputStreamTest.java new file mode 100644 index 0000000..80da272 --- /dev/null +++ b/server/container/util/src/test/java/org/apache/james/util/io/CurrentPositionInputStreamTest.java @@ -0,0 +1,120 @@ +/**************************************************************** + * 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 CurrentPositionInputStreamTest { + 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 positionInputStreamShouldNotAlterContent() { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + assertThat(currentPositionInputStream).hasSameContentAs(new ByteArrayInputStream(BYTES)); + } + + @Test + void positionInputStreamShouldNotAlterContentOfEmptyStream() { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(new byte[0])); + + assertThat(currentPositionInputStream).hasSameContentAs(new ByteArrayInputStream(new byte[0])); + } + + @Test + void positionInputStreamShouldNotAlterContentOfBigStream() { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(TWELVE_MEGABYTES)); + + assertThat(currentPositionInputStream).hasSameContentAs(new ByteArrayInputStream(TWELVE_MEGABYTES)); + } + + @Test + void getPositionShouldReturnZeroWhenEmpty() { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(new byte[0])); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(0); + } + + @Test + void getPositionShouldReturnPositionWhenReadWithABiggerBuffer() throws Exception { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + currentPositionInputStream.read(new byte[24]); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(10); + } + + @Test + void getPositionShouldReturnPositionWhenReadWithABufferHavingSamePositionThanContent() throws Exception { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + currentPositionInputStream.read(new byte[10]); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(10); + } + + @Test + void getPositionShouldReturnPositionWhenReadUsingSmallerBuffers() throws Exception { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + currentPositionInputStream.read(new byte[6]); + currentPositionInputStream.read(new byte[6]); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(10); + } + + @Test + void getPositionShouldReturnPositionWhenReadByte() { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + IntStream.range(0, 10).forEach(Throwing.intConsumer(step -> currentPositionInputStream.read())); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(10); + } + + @Test + void getPositionShouldReturnPositionWhenSkips() throws Exception { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + currentPositionInputStream.read(new byte[6]); + currentPositionInputStream.skip(6); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(10); + } + + @Test + void getPositionShouldReturnPartialRead() throws Exception { + CurrentPositionInputStream currentPositionInputStream = new CurrentPositionInputStream(new ByteArrayInputStream(BYTES)); + + currentPositionInputStream.read(new byte[6]); + + assertThat(currentPositionInputStream.getPosition()).isEqualTo(6); + } +} \ No newline at end of file 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 deleted file mode 100644 index ece2d91..0000000 --- a/server/container/util/src/test/java/org/apache/james/util/io/SizeInputStreamTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************** - * 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