FileRandomAccessOutputStream.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.archivers.zip;

  18. import java.io.IOException;
  19. import java.nio.ByteBuffer;
  20. import java.nio.channels.FileChannel;
  21. import java.nio.file.OpenOption;
  22. import java.nio.file.Path;
  23. import java.nio.file.StandardOpenOption;
  24. import java.util.Objects;

  25. /**
  26.  * {@link RandomAccessOutputStream} implementation based on a file.
  27.  */
  28. // Keep package-private; consider for Apache Commons IO.
  29. class FileRandomAccessOutputStream extends RandomAccessOutputStream {

  30.     private final FileChannel channel;

  31.     private long position;

  32.     FileRandomAccessOutputStream(final FileChannel channel) {
  33.         this.channel = Objects.requireNonNull(channel, "channel");
  34.     }

  35.     FileRandomAccessOutputStream(final Path file) throws IOException {
  36.         this(file, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
  37.     }

  38.     FileRandomAccessOutputStream(final Path file, final OpenOption... options) throws IOException {
  39.         this(FileChannel.open(file, options));
  40.     }

  41.     FileChannel channel() {
  42.         return channel;
  43.     }

  44.     @Override
  45.     public void close() throws IOException {
  46.         if (channel.isOpen()) {
  47.             channel.close();
  48.         }
  49.     }

  50.     @Override
  51.     public synchronized long position() {
  52.         return position;
  53.     }

  54.     @Override
  55.     public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
  56.         ZipIoUtil.writeFully(this.channel, ByteBuffer.wrap(b, off, len));
  57.         position += len;
  58.     }

  59.     @Override
  60.     public void writeFully(final byte[] b, final int off, final int len, final long atPosition) throws IOException {
  61.         final ByteBuffer buf = ByteBuffer.wrap(b, off, len);
  62.         for (long currentPos = atPosition; buf.hasRemaining();) {
  63.             final int written = this.channel.write(buf, currentPos);
  64.             if (written <= 0) {
  65.                 throw new IOException("Failed to fully write to file: written=" + written);
  66.             }
  67.             currentPos += written;
  68.         }
  69.     }
  70. }