Java ByteBuffer to Short Array getShortString(ByteBuffer in)

Here you can find the source of getShortString(ByteBuffer in)

Description

Read a short string from the buffer.

License

Open Source License

Parameter

Parameter Description
in The input buffer.
maxLength The maximum number of bytes to read, or 0 if not limited.

Exception

Parameter Description
BufferUnderflowException If there are not enoughbytes available in the buffer.

Return

The string.

Declaration

public static String getShortString(ByteBuffer in) 

Method Source Code

//package com.java2s;
/* vim:set softtabstop=3 shiftwidth=3 tabstop=3 expandtab tw=72:
$Id: BufferUtil.java,v 1.2 2003/04/05 15:25:37 rsdio Exp $
    //from   w  ww.j  a  v  a  2s  .  c  o m
BufferUtil: static methods for reading from a ByteBuffer.
Copyright (C) 2003  Casey Marshall <rsdio@metastatic.org>
    
This file is a part of Jarsync.
    
Jarsync is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
Jarsync 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
General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Jarsync; if not, write to the
    
   Free Software Foundation, Inc.,
   59 Temple Place, Suite 330,
   Boston, MA  02111-1307
   USA  */

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Read a short string from the buffer.
     *
     * @param in        The input buffer.
     * @param maxLength The maximum number of bytes to read, or 0 if not
     *    limited.
     * @return The string.
     * @throws BufferUnderflowException If there are not enough
     *   bytes available in the buffer.
     */
    public static String getShortString(ByteBuffer in) {
        int l = in.get() & 0xFF;
        byte[] b = new byte[l];
        try {
            in.get(b);
            return new String(b, "ISO-8859-1");
        } catch (BufferUnderflowException bue) {
            in.position(in.position() - 1);
            throw bue;
        } catch (java.io.UnsupportedEncodingException shouldNotHappen) {
        }
        return null;
    }
}

Related

  1. getShortBE(ByteBuffer b, int start, int end)
  2. getShortBE(final ByteBuffer b, final int start, final int end)
  3. getShortLength(ByteBuffer bb)
  4. getShortLength(ByteBuffer bb, int position)
  5. getShortNumeric(java.nio.ByteBuffer buffer, int len)