garydgregory commented on code in PR #455: URL: https://github.com/apache/commons-compress/pull/455#discussion_r1437697139
########## src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.commons.compress.archivers.zip; + +import org.apache.commons.compress.utils.IOUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + + +/** + * {@link RandomAccessOutputStream} implementation based on Path file. + */ +class FileRandomAccessOutputStream extends RandomAccessOutputStream { + + private final FileChannel channel; + + private long position = 0L; + + public FileRandomAccessOutputStream(Path file) throws IOException { + this(file, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + } + + public FileRandomAccessOutputStream(Path file, OpenOption... options) throws IOException { + this.channel = FileChannel.open(file, options); + } + + public FileChannel channel() { + return channel; + } + + @Override + public synchronized void write(byte[] b, int off, int len) throws IOException { + IOUtils.writeFully(this.channel, ByteBuffer.wrap(b, off, len)); + position += len; + } + + @Override + public synchronized long position() { + return position; + } + + @Override + public void writeFullyAt(final byte[] b, final int off, final int len, long atPosition) throws IOException { Review Comment: Calling the API `write` should be enough, especially if Javadocs are comprehensive. ########## src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.commons.compress.archivers.zip; + +import org.apache.commons.compress.utils.IOUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + + +/** + * {@link RandomAccessOutputStream} implementation based on Path file. + */ +class FileRandomAccessOutputStream extends RandomAccessOutputStream { + + private final FileChannel channel; + + private long position = 0L; + + public FileRandomAccessOutputStream(Path file) throws IOException { Review Comment: Use `final` where you can. ########## src/main/java/org/apache/commons/compress/utils/IOUtils.java: ########## @@ -275,6 +277,49 @@ public static byte[] readRange(final ReadableByteChannel input, final int len) t return output.toByteArray(); } + /** + * Writes full buffer to channel. + * + * @param channel + * channel to write to + * @param buf + * buffer to write + * @throws IOException + * when writing fails + */ + public static void writeFully(SeekableByteChannel channel, ByteBuffer buf) throws IOException { Review Comment: `writeFully` -> `write` or is a "partial write" expected default behavior? That would be odd. Flip the arguments such that the source comes first and the destination second. IOW, "Writes from Foo to Bar", which happens to be the phrasing you use in the Javadoc ;-) ########## src/main/java/org/apache/commons/compress/utils/IOUtils.java: ########## @@ -275,6 +277,49 @@ public static byte[] readRange(final ReadableByteChannel input, final int len) t return output.toByteArray(); } + /** + * Writes full buffer to channel. + * + * @param channel + * channel to write to + * @param buf + * buffer to write + * @throws IOException + * when writing fails + */ + public static void writeFully(SeekableByteChannel channel, ByteBuffer buf) throws IOException { + while (buf.hasRemaining()) { + int remaining = buf.remaining(); + int written = channel.write(buf); + if (written <= 0) { + throw new IOException("Failed to fully write: channel=" + channel + " length=" + remaining + " written=" + written); + } + } + } + + /** + * Writes full buffer to channel at specified position. + * + * @param channel + * channel to write to + * @param buf + * buffer to write + * @param position + * position to write at + * @throws IOException + * when writing fails + */ + public static void writeFullyAt(FileChannel channel, ByteBuffer buf, long position) throws IOException { Review Comment: See above. All new public and protected elements shoyuld have an `@since` Javadoc tag. ########## src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.commons.compress.archivers.zip; + +import org.apache.commons.compress.utils.IOUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + + +/** + * {@link RandomAccessOutputStream} implementation based on Path file. + */ +class FileRandomAccessOutputStream extends RandomAccessOutputStream { Review Comment: This new class needs a unit test to prove this class behaves as an output stream and that all APIs do what they say. ########## src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.commons.compress.archivers.zip; + +import org.apache.commons.compress.utils.IOUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.Path; + + +/** + * {@link RandomAccessOutputStream} implementation for SeekableByteChannel. + */ +class SeekableChannelRandomAccessOutputStream extends RandomAccessOutputStream { + + private final SeekableByteChannel channel; + + private long position = 0L; Review Comment: Don't initialize instance variables to their default values. ########## src/main/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStream.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.commons.compress.archivers.zip; + +import java.io.IOException; +import java.io.OutputStream; + + +/** + * Abstraction over OutputStream which also allows random access writes. + */ +abstract class RandomAccessOutputStream extends OutputStream { Review Comment: This new class needs a unit test to prove this class behaves as an output stream and that all APIs do what they say. ########## src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.commons.compress.archivers.zip; + +import org.apache.commons.compress.utils.IOUtils; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.Path; + + +/** + * {@link RandomAccessOutputStream} implementation for SeekableByteChannel. + */ +class SeekableChannelRandomAccessOutputStream extends RandomAccessOutputStream { Review Comment: This new class needs a unit test to prove this class behaves as an output stream and that all APIs do what they say. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
