| 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 | * https://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 | |
| 20 | package org.apache.commons.compress.archivers.zip; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.channels.FileChannel; |
| 25 | import java.nio.channels.WritableByteChannel; |
| 26 | |
| 27 | /** |
| 28 | * IO utilities for Zip operations. |
| 29 | */ |
| 30 | // Keep package-private; consider for Apache Commons IO. |
| 31 | final class ZipIoUtil { |
| 32 | |
| 33 | /** |
| 34 | * Writes all bytes in a buffer to a channel at specified position. |
| 35 | * |
| 36 | * @param channel The target channel. |
| 37 | * @param buffer The source bytes. |
| 38 | * @param position The file position at which the transfer is to begin; must be non-negative |
| 39 | * @throws IOException If some I/O error occurs or fails or fails to write all bytes. |
| 40 | */ |
| 41 | static void writeAll(final FileChannel channel, final ByteBuffer buffer, final long position) throws IOException { |
| 42 | for (long currentPos = position; buffer.hasRemaining();) { |
| 43 | final int remaining = buffer.remaining(); |
| 44 | final int written = channel.write(buffer, currentPos); |
| 45 | if (written == 0) { |
| 46 | // A non-blocking channel |
| 47 | Thread.yield(); |
| 48 | continue; |
| 49 | } |
| 50 | if (written < 0) { |
| 51 | throw new IOException("Failed to write all bytes in the buffer for channel=" + channel + ", length=" + remaining + ", written=" + written); |
| 52 | } |
| 53 | currentPos += written; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Writes all bytes in a buffer to a channel. |
| 59 | * |
| 60 | * @param channel The target channel. |
| 61 | * @param buffer The source bytes. |
| 62 | * @throws IOException If some I/O error occurs or fails or fails to write all bytes. |
| 63 | */ |
| 64 | static void writeAll(final WritableByteChannel channel, final ByteBuffer buffer) throws IOException { |
| 65 | while (buffer.hasRemaining()) { |
| 66 | final int remaining = buffer.remaining(); |
| 67 | final int written = channel.write(buffer); |
| 68 | if (written == 0) { |
| 69 | // A non-blocking channel |
| 70 | Thread.yield(); |
| 71 | continue; |
| 72 | } |
| 73 | if (written < 0) { |
| 74 | throw new IOException("Failed to write all bytes in the buffer for channel=" + channel + ", length=" + remaining + ", written=" + written); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private ZipIoUtil() { |
| 80 | } |
| 81 | } |