Java ByteBuffer Set getEquals(ByteBuffer buf, String s, String charsetName)

Here you can find the source of getEquals(ByteBuffer buf, String s, String charsetName)

Description

Gets a String that is the same length as the given string starting at the buffer's current position, advances the position by the same length, and compares the two strings for equality.

License

Open Source License

Parameter

Parameter Description
buf The ByteBuffer to get bytes from.
s The String to compare to.
charsetName The name of a supported character set.

Exception

Parameter Description
BufferUnderflowException if there are fewer than the number ofbytes in the given string remaining in the buffer starting at itscurrent position.

Return

Returns true only if the two strings are equal.

Declaration

public static boolean getEquals(ByteBuffer buf, String s, String charsetName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.ByteBuffer;

public class Main {
    /**/*from  w  ww . j av  a  2  s  .c  o m*/
     * Gets a {@link String} that is the same length as the given string
     * starting at the buffer's current position, advances the position by the
     * same length, and compares the two strings for equality.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param s The {@link String} to compare to.
     * @param charsetName The name of a supported character set.
     * @return Returns <code>true</code> only if the two strings are equal.
     * @throws BufferUnderflowException if there are fewer than the number of
     * bytes in the given string remaining in the buffer starting at its
     * current position.
     * @see #getEquals(ByteBuffer,int,String,String)
     */
    public static boolean getEquals(ByteBuffer buf, String s, String charsetName) {
        return s.equals(getString(buf, s.length(), charsetName));
    }

    /**
     * Gets a {@link String} that is the same length as the given string
     * starting at the given position and compares the two strings for equality.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param offset The position to start getting bytes from.
     * @param s The {@link String} to compare to.
     * @param charsetName The name of a supported character set.
     * @return Returns <code>true</code> only if the two strings are equal.
     * @throws BufferUnderflowException if there are fewer than the number of
     * bytes in the given string remaining in the buffer starting at the given
     * position.
     * @see #getEquals(ByteBuffer,String,String)
     */
    public static boolean getEquals(ByteBuffer buf, int offset, String s, String charsetName) {
        return s.equals(getString(buf, offset, s.length(), charsetName));
    }

    /**
     * This is a relative get {@link String} method for {@link ByteBuffer}
     * because it doesn't have one (but should!).
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param length The number of bytes to be copied.
     * @param charsetName The name of a supported character set.
     * @return Returns a {@link String} comprising the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes remaining in the buffer.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit, or <code>charsetName</code> specifies an
     * unsupported character set.
     */
    public static String getString(ByteBuffer buf, int length, String charsetName) {
        try {
            return new String(getBytes(buf, length), charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * This is an absolute get {@link String} method for {@link ByteBuffer}
     * because it doesn't have one (but should!).  The {@link ByteBuffer}'s
     * position is unchanged.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param offset The absolute offset within the {@link ByteBuffer} of the
     * first byte to be read; must be non-negative.
     * @param length The number of bytes to be copied.
     * @param charsetName The name of a supported character set.
     * @return Returns a {@link String} comprising the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes in the buffer starting at the given offset.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit, or <code>charsetName</code> specifies an
     * unsupported character set.
     */
    public static String getString(ByteBuffer buf, int offset, int length, String charsetName) {
        try {
            return new String(getBytes(buf, offset, length), charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * This is a relative bulk get method for {@link ByteBuffer} that returns
     * the array of bytes gotten.  It is a convenience method so the array
     * doesn't have to be declared beforehand.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param length The number of bytes to be copied.
     * @return Returns an array of the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes remaining in the buffer.
     */
    public static byte[] getBytes(ByteBuffer buf, int length) {
        final byte[] dst = new byte[length];
        buf.get(dst, 0, length);
        return dst;
    }

    /**
     * This is an absolute bulk get method for {@link ByteBuffer} because it
     * doesn't have one (but should!).  Being absolute, the
     * {@link ByteBuffer}'s position doesn't change.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param offset The absolute offset within the {@link ByteBuffer} of the
     * first byte to be read; must be non-negative.
     * @param length The number of bytes to be copied.
     * @return Returns an array of the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes in the buffer starting at the given offset.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit.
     */
    public static byte[] getBytes(ByteBuffer buf, int offset, int length) {
        final byte[] dst = new byte[length];
        getBytes(buf, offset, dst, 0, length);
        return dst;
    }

    /**
     * This is an absolute bulk get method for {@link ByteBuffer} because it
     * doesn't have one (but should!).  Being absolute, the
     * {@link ByteBuffer}'s position doesn't change.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param bufOffset The absolute offset within the {@link ByteBuffer} of
     * the first byte to be read; must be non-negative.
     * @param dst The array into which bytes are to be written.
     * @param dstOffset The offset within the destination array to start
     * writing bytes.
     * @param length The number of bytes to be copied.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes in the buffer starting at the given offset.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit.
     */
    public static void getBytes(ByteBuffer buf, int bufOffset, byte[] dst, int dstOffset, int length) {
        final int origPos = buf.position();
        buf.position(bufOffset);
        buf.get(dst, dstOffset, length);
        buf.position(origPos);
    }
}

Related

  1. findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)
  2. from(ByteBuffer buffer, int offset)
  3. fromListToSetByteArray(List list)
  4. get(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)
  5. getCharsetFromDocument(ByteBuffer bb)
  6. getSignedInt(ByteBuffer buffer, int offset)
  7. hash_murmur3_128(ByteBuffer buf, int offset, int size, int i, byte[] result)
  8. isFree(int frameIx, int offset, ByteBuffer[] frames)
  9. macAddressToString(ByteBuffer packet, int offset, boolean needHyphen)