Java File Read via ByteBuffer readShortBuffer(DataInputStream input)

Here you can find the source of readShortBuffer(DataInputStream input)

Description

read Short Buffer

License

Open Source License

Parameter

Parameter Description
input a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static ByteBuffer readShortBuffer(DataInputStream input) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w w w .j  ava 2s  .c  om*/
 * Copyright 2016 LinkedIn Corp. All rights reserved.
 *
 * 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.
 */

import java.io.DataInputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**
     *
     * @param input
     * @return
     * @throws IOException
     */
    public static ByteBuffer readShortBuffer(DataInputStream input) throws IOException {
        short size = input.readShort();
        if (size < 0) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate(size);
        int read = 0;
        while (read < size) {
            int readBytes = input.read(buffer.array());
            if (readBytes == -1 || readBytes == 0) {
                break;
            }
            read += readBytes;
        }
        if (read != size) {
            throw new IllegalArgumentException(
                    "readShortBuffer the size of the input does not match the actual data size");
        }
        return buffer;
    }
}

Related

  1. readLTriad(byte[] triad)
  2. readNByte(ReadableByteChannel channel, int n)
  3. readNIOFile(String filePath)
  4. readShort(byte[] bytes, int start)
  5. readShort(DataInput input)
  6. readShortByteArray(DataInput in)
  7. readString(ByteArrayInputStream bin)
  8. readString(ReadableByteChannel channel)
  9. readStringFromFile(String path)