Java ByteBuffer to Int readInts4(final ByteBuffer buffer, final int[] array, final int count)

Here you can find the source of readInts4(final ByteBuffer buffer, final int[] array, final int count)

Description

read Ints

License

Open Source License

Declaration

public static void readInts4(final ByteBuffer buffer, final int[] array, final int count) throws IOException 

Method Source Code


//package com.java2s;
/*/*w ww  .jav a  2s  .c  om*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2017 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
 * or LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    public static void readInts4(final InputStream is, final int[] array, final int count) throws IOException {
        readInts4(is, array, count, 0);
    }

    public static int readInts4(final InputStream is, final int[] array, final int count, final int lowValue)
            throws IOException {
        int value = 0;
        int octet = 0;
        int readInts = 0;
        int shVal = 0;
        int neeble = 0;
        int neebleNum = 0;

        if (lowValue > 0) {
            octet = lowValue & 0xF;
            neebleNum = 1;
        }

        for (; readInts < count; neebleNum++) {
            if (neebleNum % 2 == 0) {
                octet = is.read();
                if (octet == -1) {
                    throw new EOFException();
                }
                neeble = octet >> 4;
            } else {
                neeble = octet & 0xF;
            }

            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) == 0) {
                array[readInts++] = value;
                shVal = 0;
                value = 0;
            } else {
                shVal += 3;
            }
        }

        if (neebleNum % 2 != 0) {
            return 0x80 | (octet & 0xF);
        }

        return 0;
    }

    public static void readInts4(final ByteBuffer buffer, final int[] array, final int count) throws IOException {
        readInts4(buffer, array, count, 0);
    }

    public static int readInts4(final ByteBuffer buffer, final int[] array, final int count, final int lowValue)
            throws IOException {
        int value = 0;
        int octet = 0;
        int readInts = 0;
        int shVal = 0;
        int neeble = 0;
        int neebleNum = 0;

        if (lowValue > 0) {
            octet = lowValue & 0xF;
            neebleNum = 1;
        }

        for (; readInts < count; neebleNum++) {
            if (neebleNum % 2 == 0) {
                if (!buffer.hasRemaining()) {
                    throw new EOFException();
                }
                octet = buffer.get();

                neeble = octet >> 4;
            } else {
                neeble = octet & 0xF;
            }

            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) == 0) {
                array[readInts++] = value;
                shVal = 0;
                value = 0;
            } else {
                shVal += 3;
            }
        }

        if (neebleNum % 2 != 0) {
            return 0x80 | (octet & 0xF);
        }

        return 0;
    }
}

Related

  1. readIntEquals(ByteBuffer buf, int i, int j)
  2. readIntLE(ByteBuffer buf, int i)
  3. readIntMSB(ByteBuffer logBuf)
  4. readIntoBuffer(SocketChannel channel, ByteBuffer buf, int sleepMsecs)
  5. readInts(final ByteBuffer bb, final int length)
  6. readUnsigned(ByteBuffer in, int size)
  7. readUnsignedByte(ByteBuffer buffer)
  8. readUnsignedInt(ByteBuffer buf)
  9. readUnsignedInt(ByteBuffer buffer)