CircularBuffer.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. /**
  21.  * Circular byte buffer.
  22.  *
  23.  * @since 1.7
  24.  */
  25. final class CircularBuffer {

  26.     /** Size of the buffer */
  27.     private final int size;

  28.     /** The buffer */
  29.     private final byte[] buffer;

  30.     /** Index of the next data to be read from the buffer */
  31.     private int readIndex;

  32.     /** Index of the next data written in the buffer */
  33.     private int writeIndex;

  34.     CircularBuffer(final int size) {
  35.         this.size = size;
  36.         buffer = new byte[size];
  37.     }

  38.     /**
  39.      * Tests whether a new byte can be read from the buffer.
  40.      *
  41.      * @return Whether a new byte can be read from the buffer.
  42.      */
  43.     public boolean available() {
  44.         return readIndex != writeIndex;
  45.     }

  46.     /**
  47.      * Copies a previous interval in the buffer to the current position.
  48.      *
  49.      * @param distance the distance from the current write position
  50.      * @param length   the number of bytes to copy
  51.      */
  52.     public void copy(final int distance, final int length) {
  53.         final int pos1 = writeIndex - distance;
  54.         final int pos2 = pos1 + length;
  55.         for (int i = pos1; i < pos2; i++) {
  56.             buffer[writeIndex] = buffer[(i + size) % size];
  57.             writeIndex = (writeIndex + 1) % size;
  58.         }
  59.     }

  60.     /**
  61.      * Reads a byte from the buffer.
  62.      *
  63.      * @return a byte from the buffer.
  64.      */
  65.     public int get() {
  66.         if (available()) {
  67.             final int value = buffer[readIndex];
  68.             readIndex = (readIndex + 1) % size;
  69.             return value & 0xFF;
  70.         }
  71.         return -1;
  72.     }

  73.     /**
  74.      * Puts a byte to the buffer.
  75.      *
  76.      * @param value the value to put.
  77.      */
  78.     public void put(final int value) {
  79.         buffer[writeIndex] = (byte) value;
  80.         writeIndex = (writeIndex + 1) % size;
  81.     }
  82. }