Java examples for java.nio:ByteBuffer String
Puts a String 's bytes in the character set starting at the ByteBuffer 's current position.
//package com.java2s; import java.io.*; import java.nio.ByteBuffer; public class Main { /**// w w w.ja v a 2s.co m * Puts a {@link String}'s bytes in the character set starting at the * {@link ByteBuffer}'s current position. The buffer's position is * advanced by the number of bytes put. * <p> * This is just a convenience method for not having to deal with the * {@link UnsupportedEncodingException}. * * @param buf The {@link ByteBuffer} to put the {@link String} into. * @param s The {@link String} to put. * @param charsetName The name of a supported character set. * @throws BufferOverflowException if there is insufficient space remaining * in the buffer. * @throws IllegalArgumentException if <code>charsetName</code> is an * unsupported character set. * @see #put(ByteBuffer,int,String,String) */ public static void put(ByteBuffer buf, String s, String charsetName) { try { buf.put(s.getBytes(charsetName)); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } } /** * Puts a {@link String}'s bytes in the given character set at the given * absolute position. Being absolute, the {@link ByteBuffer}'s position * doesn't change. * * @param buf The {@link ByteBuffer} to put the {@link String} into. * @param offset The absolute offset within the {@link ByteBuffer} of where * to put the {@link String}; must be non-negative. * @param s The {@link String} to put. * @param charsetName The name of a supported character set. * @throws BufferOverflowException if there is insufficient space in the * buffer starting at the given position. * @throws IllegalArgumentException if the offset is either negative or * beyond the buffer's limit, or <code>charsetName</code> specifies an * unsupported character set. * @see #put(ByteBuffer,String,String) */ public static void put(ByteBuffer buf, int offset, String s, String charsetName) { final int origPos = buf.position(); buf.position(offset); put(buf, s, charsetName); buf.position(origPos); } /** * 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); } }