Android Byte Array Encode putOrCopyString(String fromString, byte[] toBytes)

Here you can find the source of putOrCopyString(String fromString, byte[] toBytes)

Description

put Or Copy String

License

Open Source License

Declaration

public static byte[] putOrCopyString(String fromString, byte[] toBytes) 

Method Source Code

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

import java.io.UnsupportedEncodingException;

public class Main {
    public static final String BYTE_CHARSET = "UTF-8";

    public static byte[] putOrCopyString(String fromString, byte[] toBytes) {
        byte[] fromBytes;

        try {//from   w w w .j  av  a  2s . c o  m
            fromBytes = fromString.getBytes(BYTE_CHARSET);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        if (toBytes.length == fromBytes.length) {
            System.arraycopy(fromBytes, 0, toBytes, 0, fromBytes.length);
        } else {
            toBytes = fromBytes;
        }

        return fromBytes;
    }
}

Related

  1. putChar(byte[] bb, char ch, int index)
  2. putDouble(byte[] bb, double x, int index)
  3. putInt(int value, int offset, byte[] byteArr)
  4. putInts(int[] fromInts, byte[] toBytes)
  5. putLong(byte[] bb, long x, int index)
  6. putReverseBytesInt(byte[] bb, int x, int index)
  7. putReverseBytesLong(byte[] bb, long x, int index)