Java ByteBuffer Serialize serializeString(final String value, ByteBuffer buffer)

Here you can find the source of serializeString(final String value, ByteBuffer buffer)

Description

serialize String

License

Open Source License

Declaration

public final static void serializeString(final String value, ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/**/* w ww.j  a  v  a2  s  .c o  m*/
 * Copyright - See the COPYRIGHT that is included with this distribution.
 * EPICS pvData is distributed subject to a Software License Agreement found
 * in file LICENSE that is included with this distribution.
 */

import java.nio.ByteBuffer;

public class Main {
    public final static void serializeString(final String value, ByteBuffer buffer) {
        if (value == null)
            writeSize(-1, buffer);
        else {
            final int len = value.length();
            writeSize(len, buffer);
            buffer.put(value.getBytes()); // UTF-8
        }
    }

    public final static void writeSize(final int s, ByteBuffer buffer) {
        if (s == -1) // null
            buffer.put((byte) -1);
        else if (s < 254)
            buffer.put((byte) s);
        else
            buffer.put((byte) -2).putInt(s); // (byte)-2 + size
    }
}

Related

  1. deserializeBoolean(ByteBuffer buf)
  2. deserializeFromByteBufferNoHeader(ByteBuffer bytes)
  3. deserializeObject(ByteBuffer obj)
  4. serializeBigDecimal(BigDecimal bd, ByteBuffer buf)
  5. serializeNull(ByteBuffer buf)
  6. serializeStringArray(ByteBuffer buf, String[] strArray)