ZipIoUtil.java

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

  20. import java.io.IOException;
  21. import java.nio.ByteBuffer;
  22. import java.nio.channels.FileChannel;
  23. import java.nio.channels.SeekableByteChannel;

  24. /**
  25.  * IO utilities for Zip operations.
  26.  */
  27. // Keep package-private; consider for Apache Commons IO.
  28. class ZipIoUtil {

  29.     /**
  30.      * Writes full buffer to channel.
  31.      *
  32.      * @param channel channel to write to
  33.      * @param buf     buffer to write
  34.      * @throws IOException when writing fails or fails to write fully
  35.      */
  36.     static void writeFully(final SeekableByteChannel channel, final ByteBuffer buf) throws IOException {
  37.         while (buf.hasRemaining()) {
  38.             final int remaining = buf.remaining();
  39.             final int written = channel.write(buf);
  40.             if (written <= 0) {
  41.                 throw new IOException("Failed to fully write: channel=" + channel + " length=" + remaining + " written=" + written);
  42.             }
  43.         }
  44.     }

  45.     /**
  46.      * Writes full buffer to channel at specified position.
  47.      *
  48.      * @param channel  channel to write to
  49.      * @param buf      buffer to write
  50.      * @param position position to write at
  51.      * @throws IOException when writing fails or fails to write fully
  52.      */
  53.     static void writeFullyAt(final FileChannel channel, final ByteBuffer buf, final long position) throws IOException {
  54.         for (long currentPosition = position; buf.hasRemaining();) {
  55.             final int remaining = buf.remaining();
  56.             final int written = channel.write(buf, currentPosition);
  57.             if (written <= 0) {
  58.                 throw new IOException("Failed to fully write: channel=" + channel + " length=" + remaining + " written=" + written);
  59.             }
  60.             currentPosition += written;
  61.         }
  62.     }

  63.     private ZipIoUtil() {
  64.     }
  65. }