kvr000 commented on code in PR #455: URL: https://github.com/apache/commons-compress/pull/455#discussion_r1438716334
########## 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: See the comment later - the default semantics on `SeekableByteChannel` is to write part of the buffer, not necessarily full. It's consistent with naming convention in other libraries such as https://hadoop.apache.org/docs/stable/api/org/apache/hadoop/io/IOUtils.html#writeFully-java.nio.channels.FileChannel-java.nio.ByteBuffer-long- -- 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]
