All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.commons.io.streams.BitInputStream Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed 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 com.phloc.commons.io.streams;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Nonnull;

/**
 * The {@link BitInputStream} allows reading individual bits from a general Java
 * InputStream. Like the various Stream-classes from Java, the BitInputStream
 * has to be created based on another Input stream. It provides a function to
 * read the next bit from the stream, as well as to read multiple bits at once
 * and write the resulting data into an integer value.
* For a non-blocking version see {@link NonBlockingBitInputStream}. * * @author Andreas Jakl * @author Philip Helger */ public class BitInputStream extends NonBlockingBitInputStream { private final Lock m_aLock = new ReentrantLock (); /** * Create a new bit input stream based on an existing Java InputStream. * * @param aIS * the input stream this class should read the bits from. May not be * null. * @param aByteOrder * The non-null byte order to use. */ public BitInputStream (@Nonnull final InputStream aIS, @Nonnull final ByteOrder aByteOrder) { super (aIS, aByteOrder); } /** * Read the next bit from the stream. * * @return 0 if the bit is 0, 1 if the bit is 1. * @throws IOException * In case EOF is reached */ @Override public int readBit () throws IOException { m_aLock.lock (); try { return super.readBit (); } finally { m_aLock.unlock (); } } /** * Close the underlying input stream. */ @Override public void close () { m_aLock.lock (); try { super.close (); } finally { m_aLock.unlock (); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy