Java ByteBuffer Put putAscii(ByteBuffer bytes, String value)

Here you can find the source of putAscii(ByteBuffer bytes, String value)

Description

put Ascii

License

Open Source License

Declaration

public static ByteBuffer putAscii(ByteBuffer bytes, String value) throws IOException 

Method Source Code


//package com.java2s;
/*//from w  w  w .  j  a  v  a  2  s  . c o  m
Copyright 2011 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.nio.ByteBuffer;
import java.io.IOException;

public class Main {
    public static ByteBuffer putAscii(ByteBuffer bytes, String value) throws IOException {
        byte[] asciiBytes = value.getBytes("US-ASCII");
        int numBytes = asciiBytes.length;
        if (numBytes > 65535)
            throw new IOException("ASCII string exceeds maximum size of 65535 bytes!");

        if (bytes.remaining() < (numBytes + 2))
            throw new IOException("not enough space in buffer to write string value");

        putUnsignedShort(bytes, numBytes);
        for (int i = 0; i < numBytes; i++)
            bytes.put(asciiBytes[i]);

        return bytes;
    }

    public static ByteBuffer putAscii(ByteBuffer bytes, int pos, String value) throws IOException {
        byte[] asciiBytes = value.getBytes("US-ASCII");
        int numBytes = asciiBytes.length;
        if (numBytes > 65535)
            throw new IOException("ASCII string exceeds maximum size of 65535 bytes!");

        if ((pos + numBytes + 2) > bytes.limit())
            throw new IOException("not enough space in buffer to write string value");

        putUnsignedShort(bytes, pos, numBytes);
        pos += 2;
        for (int i = 0; i < numBytes; i++)
            bytes.put(pos + i, asciiBytes[i]);

        return bytes;
    }

    public static ByteBuffer putUnsignedShort(ByteBuffer bytes, int value) throws IOException {
        if (bytes.remaining() < 2)
            throw new IOException("not enough space in buffer to write unsigned short");

        if (value > 65535)
            throw new IOException("int value exceeds maximum unsigned short value of 65535!");

        byte b1 = (byte) (0xff & (value >> 8));
        byte b2 = (byte) (0xff & value);

        bytes.put(b1);
        bytes.put(b2);

        return bytes;
    }

    public static ByteBuffer putUnsignedShort(ByteBuffer bytes, int pos, int value) throws IOException {
        if ((pos + 2) > bytes.limit())
            throw new IOException("not enough space in buffer to write unsigned short");

        if (value > 65535)
            throw new IOException("int value exceeds maximum unsigned short value of 65535!");

        byte b1 = (byte) (0xff & (value >> 8));
        byte b2 = (byte) (0xff & value);

        bytes.put(pos, b1);
        bytes.put(pos + 1, b2);

        return bytes;
    }
}

Related

  1. put(ByteBuffer buf, String s, String charsetName)
  2. put(ByteBuffer dst, ByteBuffer src, int transferLimit)
  3. put(ByteBuffer from, ByteBuffer to)
  4. put(ByteBuffer src, ByteBuffer dst)
  5. put3ByteInt(ByteBuffer buffer, int val)
  6. putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src)
  7. putBoolean(ByteBuffer bb, boolean value)
  8. putByte(ByteBuffer byteBuffer, int value)
  9. putByteArray(ByteBuffer byteBuffer, byte[] array)