This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch spring6 in repository https://gitbox.apache.org/repos/asf/causeway.git
commit cd30f5ed4fb888df263ff89b362ef4ec4db04889 Merge: eba0c2acf3 68dc038666 Author: Andi Huber <[email protected]> AuthorDate: Mon Feb 27 13:43:29 2023 +0100 CAUSEWAY-3304: [Commons] Code Quality Improvements for 2.0.0 Release .../org/apache/causeway/commons/io/DataPeer.java | 75 +++++++++++++ .../org/apache/causeway/commons/io/DataSink.java | 69 +++++++----- .../org/apache/causeway/commons/io/DataSource.java | 125 +++++++++++++++++++-- .../org/apache/causeway/commons/io/JaxbUtils.java | 4 +- .../org/apache/causeway/commons/io/JsonUtils.java | 4 +- .../org/apache/causeway/commons/io/YamlUtils.java | 2 +- 6 files changed, 236 insertions(+), 43 deletions(-) diff --cc commons/src/main/java/org/apache/causeway/commons/io/DataPeer.java index 0000000000,3d8827ed8a..b27e4bb061 mode 000000,100644..100644 --- a/commons/src/main/java/org/apache/causeway/commons/io/DataPeer.java +++ b/commons/src/main/java/org/apache/causeway/commons/io/DataPeer.java @@@ -1,0 -1,74 +1,75 @@@ + /* + * 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.causeway.commons.io; + + import java.io.InputStream; + import java.io.OutputStream; + import java.util.ArrayList; + import java.util.function.Function; + -import org.apache.causeway.commons.functional.ThrowingConsumer; ++import org.springframework.util.function.ThrowingConsumer; ++ + import org.apache.causeway.commons.functional.Try; + import org.apache.causeway.commons.internal.exceptions._Exceptions; + + import lombok.NonNull; + import lombok.val; + + /** + * General purpose byte data peer that acts as source and sink at the same time. + * + * @since 2.0 {@index} + */ + public interface DataPeer extends DataSink, DataSource { + + static DataPeer inMemory() { + return inMemory(32); + } + + static DataPeer inMemory(final int initialBufferSize) { + + val byteArrayHolder = new ArrayList<byte[]>(1); + + return new DataPeer() { + @Override + public <T> Try<T> tryReadAll(@NonNull final Function<InputStream, Try<T>> consumingMapper) { + val in = DataSource.ofBytes(bytes()); + return in.tryReadAll(consumingMapper); + } + + @Override + public void writeAll(@NonNull final ThrowingConsumer<OutputStream> outputStreamConsumer) { + if(!byteArrayHolder.isEmpty()) { + throw _Exceptions.illegalState("Cannot writeAll to an in-memory DataPeer, that was already written to."); + } + val out = DataSink.ofByteArrayConsumer(byteArrayHolder::add, initialBufferSize); + out.writeAll(outputStreamConsumer); + } + + @Override + public byte[] bytes() { + return byteArrayHolder.isEmpty() + ? new byte[0] + : byteArrayHolder.get(0); + } + + }; + } + + } diff --cc commons/src/main/java/org/apache/causeway/commons/io/DataSink.java index a6eac0a622,642a1bd22e..f61c8eae26 --- a/commons/src/main/java/org/apache/causeway/commons/io/DataSink.java +++ b/commons/src/main/java/org/apache/causeway/commons/io/DataSink.java @@@ -25,9 -25,9 +25,10 @@@ import java.io.OutputStream import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.function.Consumer; - import java.util.function.Function; - import java.util.function.Supplier; + -import org.apache.causeway.commons.functional.ThrowingConsumer; -import org.apache.causeway.commons.functional.ThrowingSupplier; ++import org.springframework.util.function.ThrowingConsumer; ++import org.springframework.util.function.ThrowingSupplier; + import org.apache.causeway.commons.functional.Try; import lombok.NonNull; diff --cc commons/src/main/java/org/apache/causeway/commons/io/DataSource.java index 5d150b9c80,ac56c99bff..cc668039b4 --- a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java +++ b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java @@@ -26,17 -27,23 +27,23 @@@ import java.nio.charset.Charset import java.nio.charset.StandardCharsets; import java.util.function.Consumer; import java.util.function.Function; - import java.util.function.Supplier; + + import javax.imageio.ImageIO; import org.springframework.lang.Nullable; +import org.springframework.util.function.ThrowingConsumer; +import org.springframework.util.function.ThrowingFunction; ++import org.springframework.util.function.ThrowingSupplier; + import org.apache.causeway.commons.collections.Can; -import org.apache.causeway.commons.functional.ThrowingConsumer; -import org.apache.causeway.commons.functional.ThrowingFunction; -import org.apache.causeway.commons.functional.ThrowingSupplier; import org.apache.causeway.commons.functional.Try; + import org.apache.causeway.commons.internal.base._Bytes; import org.apache.causeway.commons.internal.base._NullSafe; import org.apache.causeway.commons.internal.base._Strings; + import org.apache.causeway.commons.internal.base._Text; import lombok.NonNull; + import lombok.val; /** * General purpose readable byte data source.
