// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstants.java
// static int mask(final int size) {
//     return MASKS[size - 1];
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeByte(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Byte.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Byte.SIZE);
//     }
//     if (unsigned && size == Byte.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned byte");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeChar(final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Character.SIZE) {
//         throw new IllegalArgumentException("size(" + size + ") > " + Character.SIZE);
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeInt(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Integer.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Integer.SIZE);
//     }
//     if (unsigned && size == Integer.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned integer");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeLong(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Long.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Long.SIZE);
//     }
//     if (unsigned && size == Long.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned long");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeShort(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Short.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Short.SIZE);
//     }
//     if (unsigned && size == Short.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned short");
//     }
//     return size;
// }


import java.io.IOException;
import static com.github.jinahya.bit.io.BitIoConstants.mask;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeByte;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeChar;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeInt;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeLong;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeShort;
package com.github.jinahya.bit.io;

/*-
 * #%L
 * bit-io
 * %%
 * Copyright (C) 2014 - 2019 Jinahya, Inc.
 * %%
 * 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.
 * #L%
 */



/**
 * An abstract class for implementing {@link BitInput} interface.
 *
 * @author Jin Kwon &lt;jinahya_at_gmail.com&gt;
 * @see AbstractBitInput
 * @see DefaultBitOutput
 */
public abstract class AbstractBitOutput implements BitOutput {

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Creates a new instance.
     */
    protected AbstractBitOutput() {
        super();
    }

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Writes given {@value java.lang.Byte#SIZE}-bit unsigned integer.
     *
     * @param value the {@value java.lang.Byte#SIZE}-bit unsigned integer to write.
     * @throws IOException if an I/O error occurs.
     */
    protected abstract void write(int value) throws IOException;

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Writes an unsigned {@code int} value of specified bit size which is, in maximum, {@value java.lang.Byte#SIZE}.
     *
     * @param size  the number of lower bits to write; between {@code 1} and {@value java.lang.Byte#SIZE}, both
     *              inclusive.
     * @param value the value to write.
     * @throws IOException if an I/O error occurs.
     * @see #write(int)
     */
    private void unsigned8(final int size, final int value) throws IOException {
        final int required = size - available;
        if (required > 0) {
            unsigned8(available, value >> required);
            unsigned8(required, value);
            return;
        }
        octet <<= size;
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstants.java
// static int mask(final int size) {
//     return MASKS[size - 1];
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeByte(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Byte.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Byte.SIZE);
//     }
//     if (unsigned && size == Byte.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned byte");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeChar(final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Character.SIZE) {
//         throw new IllegalArgumentException("size(" + size + ") > " + Character.SIZE);
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeInt(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Integer.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Integer.SIZE);
//     }
//     if (unsigned && size == Integer.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned integer");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeLong(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Long.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Long.SIZE);
//     }
//     if (unsigned && size == Long.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned long");
//     }
//     return size;
// }
// 
// Path: src/main/java/com/github/jinahya/bit/io/BitIoConstraints.java
// static int requireValidSizeShort(final boolean unsigned, final int size) {
//     if (size <= 0) {
//         throw new IllegalArgumentException("size(" + size + ") <= 0");
//     }
//     if (size > Short.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") > " + Short.SIZE);
//     }
//     if (unsigned && size == Short.SIZE) {
//         throw new IllegalArgumentException("invalid size(" + size + ") for unsigned short");
//     }
//     return size;
// }


// Path: src/main/java/com/github/jinahya/bit/io/AbstractBitOutput.java
import java.io.IOException;
import static com.github.jinahya.bit.io.BitIoConstants.mask;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeByte;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeChar;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeInt;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeLong;
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeShort;

package com.github.jinahya.bit.io;

/*-
 * #%L
 * bit-io
 * %%
 * Copyright (C) 2014 - 2019 Jinahya, Inc.
 * %%
 * 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.
 * #L%
 */



/**
 * An abstract class for implementing {@link BitInput} interface.
 *
 * @author Jin Kwon &lt;jinahya_at_gmail.com&gt;
 * @see AbstractBitInput
 * @see DefaultBitOutput
 */
public abstract class AbstractBitOutput implements BitOutput {

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Creates a new instance.
     */
    protected AbstractBitOutput() {
        super();
    }

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Writes given {@value java.lang.Byte#SIZE}-bit unsigned integer.
     *
     * @param value the {@value java.lang.Byte#SIZE}-bit unsigned integer to write.
     * @throws IOException if an I/O error occurs.
     */
    protected abstract void write(int value) throws IOException;

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * Writes an unsigned {@code int} value of specified bit size which is, in maximum, {@value java.lang.Byte#SIZE}.
     *
     * @param size  the number of lower bits to write; between {@code 1} and {@value java.lang.Byte#SIZE}, both
     *              inclusive.
     * @param value the value to write.
     * @throws IOException if an I/O error occurs.
     * @see #write(int)
     */
    private void unsigned8(final int size, final int value) throws IOException {
        final int required = size - available;
        if (required > 0) {
            unsigned8(available, value >> required);
            unsigned8(required, value);
            return;
        }
        octet <<= size;
        octet |= value & mask(size);