com.cedarsoftware.util.convert.CharArrayConversions Maven / Gradle / Ivy
The newest version!
package com.cedarsoftware.util.convert;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Arrays;
/**
* @author Kenny Partlow ([email protected])
*
* Copyright (c) Cedar Software LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* License
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class CharArrayConversions {
private CharArrayConversions() {}
static ByteBuffer toByteBuffer(Object from, Converter converter) {
return converter.getOptions().getCharset().encode(toCharBuffer(from, converter));
}
static String toString(Object from, Converter converter) {
char[] chars = (char[]) from;
return new String(chars);
}
static CharBuffer toCharBuffer(Object from, Converter converter) {
char[] chars = (char[]) from;
return CharBuffer.wrap(chars);
}
static StringBuffer toStringBuffer(Object from, Converter converter) {
return new StringBuffer(toCharBuffer(from, converter));
}
static StringBuilder toStringBuilder(Object from, Converter converter) {
return new StringBuilder(toCharBuffer(from, converter));
}
static byte[] toByteArray(Object from, Converter converter) {
ByteBuffer buffer = toByteBuffer(from, converter);
byte[] byteArray = new byte[buffer.remaining()];
buffer.get(byteArray);
return byteArray;
}
static char[] toCharArray(Object from, Converter converter) {
char[] chars = (char[])from;
return Arrays.copyOf(chars, chars.length);
}
}