Java ByteBuffer from String toByteBuffer(String value, String charsetName)

Here you can find the source of toByteBuffer(String value, String charsetName)

Description

convert a String to a ByteBuffer

License

Open Source License

Declaration

static public ByteBuffer toByteBuffer(String value, String charsetName) 

Method Source Code

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

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutputStream;

import java.nio.ByteBuffer;

public class Main {
    /**/*  ww  w  . jav a 2s. c o m*/
     * convert a String to a ByteBuffer
     */
    static public ByteBuffer toByteBuffer(String value, String charsetName) {
        ByteBuffer result = null;
        try {
            result = ByteBuffer.wrap(value.getBytes(charsetName));
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * convert a boolean to a ByteBuffer
     */
    static public ByteBuffer toByteBuffer(boolean value) {
        if (value) {
            return toByteBuffer(1, true);
        }
        return toByteBuffer(0, true);
    }

    /**
     * convert an int to a ByteBuffer
     */
    static public ByteBuffer toByteBuffer(int value, boolean variableLen) {
        ByteBuffer result = ByteBuffer.allocate(4);
        result.putInt(value);
        result.flip();
        if (variableLen) {
            int position = 0;
            int b = 0;
            for (int i = 0; i < 4; i++) {
                b = result.get();
                if (value > 0) {
                    if (b > 0) {
                        position = i;
                        break;
                    } else if (b < 0) {
                        position = i - 1;
                        break;
                    }
                } else if (value == -1) {
                    position = 3;
                } else {
                    if (b < -1) {
                        position = i;
                        break;
                    } else if (b > -1) {
                        position = i - 1;
                        break;
                    }
                }
            }
            result.rewind();
            result.position(position);
            // System.out.println("position:" + result.position() + " limit: " + result.limit() + " remaining: " + result.remaining());
        }
        return result;
    }

    /**
     * convert a long to a ByteBuffer
     */
    static public ByteBuffer toByteBuffer(long value, boolean variableLen) {
        ByteBuffer result = ByteBuffer.allocate(8);
        result.putLong(value);
        result.flip();
        if (variableLen) {
            int position = 0;
            int b = 0;
            for (int i = 0; i < 8; i++) {
                b = result.get();
                if (value > 0) {
                    if (b > 0) {
                        position = i;
                        break;
                    } else if (b < 0) {
                        position = i - 1;
                        break;
                    }
                } else if (value == -1) {
                    position = 7;
                } else {
                    if (b < -1) {
                        position = i;
                        break;
                    } else if (b > -1) {
                        position = i - 1;
                        break;
                    }
                }
            }
            result.rewind();
            result.position(position);
            // System.out.println("position:" + result.position() + " limit: " + result.limit() + " remaining: " + result.remaining());
        }
        return result;
    }

    /**
     * convert an Object to a ByteBuffer
     */
    static public ByteBuffer toByteBuffer(Object value) {
        ByteBuffer result = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(bos);
            os.writeObject(value);
            result = ByteBuffer.wrap(bos.toByteArray());
        } catch (Exception e) {
        }
        return result;
    }
}

Related

  1. toByteBuffer(final String s)
  2. toByteBuffer(String hexStr)
  3. toByteBuffer(String s)
  4. toByteBuffer(String value)
  5. toByteBuffer(String value)