/*
* Copyright 2014, Stratio.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.stratio.cassandra.util;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.CompositeType;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.Hex;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class with some {@link java.nio.ByteBuffer}/ {@link org.apache.cassandra.db.marshal.AbstractType} utilities.
*
* @author Andres de la Pena <adelapena@stratio.com>
*/
public class ByteBufferUtils {
/**
* Returns the specified {@link java.nio.ByteBuffer} as a byte array.
*
* @param byteBuffer a {@link java.nio.ByteBuffer} to be converted to a byte array.
* @return the byte array representation of the {@code byteBuffer}.
*/
public static byte[] asArray(ByteBuffer byteBuffer) {
ByteBuffer bb = ByteBufferUtil.clone(byteBuffer);
byte[] bytes = new byte[bb.remaining()];
bb.get(bytes);
return bytes;
}
public static boolean isEmpty(ByteBuffer byteBuffer) {
return byteBuffer.remaining() == 0;
}
/**
* Returns the {@link AbstractType}s contained in {@code type}.
*
* @param type the {@link AbstractType} to be split.
* @return the {@link AbstractType}s contained in {@code type}.
*/
public static List<AbstractType<?>> split(AbstractType<?> type) {
if (type instanceof CompositeType) {
return type.getComponents();
} else {
List<AbstractType<?>> result = new ArrayList<>(1);
result.add(type);
return result;
}
}
/**
* Returns the {@link java.nio.ByteBuffer}s contained in {@code byteBuffer} according to {@code type}.
*
* @param byteBuffer the {@link java.nio.ByteBuffer} to be split.
* @param type the {@link AbstractType} of {@code byteBuffer}.
* @return the {@link java.nio.ByteBuffer}s contained in {@code byteBuffer} according to {@code type}.
*/
public static ByteBuffer[] split(ByteBuffer byteBuffer, AbstractType<?> type) {
if (type instanceof CompositeType) {
return ((CompositeType) type).split(byteBuffer);
} else {
return new ByteBuffer[]{byteBuffer};
}
}
/**
* Returns a {@code String} representation of {@code byteBuffer} validated by {@code type}.
*
* @param byteBuffer the {@link java.nio.ByteBuffer} to be converted to {@code String}.
* @param type {@link AbstractType} of {@code byteBuffer}.
* @return a {@code String} representation of {@code byteBuffer} validated by {@code type}.
*/
public static String toString(ByteBuffer byteBuffer, AbstractType<?> type) {
if (type instanceof CompositeType) {
CompositeType composite = (CompositeType) type;
List<AbstractType<?>> types = composite.types;
ByteBuffer[] components = composite.split(byteBuffer);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < components.length; i++) {
AbstractType<?> componentType = types.get(i);
ByteBuffer component = components[i];
sb.append(componentType.compose(component));
if (i < types.size() - 1) {
sb.append(':');
}
}
return sb.toString();
} else {
return type.compose(byteBuffer).toString();
}
}
/**
* Returns a {@code String} representation of {@code byteBuffer}.
*
* @param byteBuffer the {@link java.nio.ByteBuffer} to be converted to {@link String}.
* @return a {@code String} representation of {@code byteBuffer}.
*/
public static String toString(ByteBuffer byteBuffer) {
return Base256Serializer.string(byteBuffer);
}
/**
* Returns the {@link java.nio.ByteBuffer} represented by {@code string}, which must be have generated by {@link
* #toString(ByteBuffer)}.
*
* @param string the {@link String} to be converted to {@link ByteBuffer}. This must be have generated by {@link
* #toString(ByteBuffer)}.
* @return the {@link java.nio.ByteBuffer} represented by {@code string}.
*/
public static ByteBuffer fromString(String string) {
return Base256Serializer.byteBuffer(string);
}
public static String toHex(ByteBuffer byteBuffer) {
return ByteBufferUtil.bytesToHex(byteBuffer);
}
public static String toHex(byte[] bytes) {
return Hex.bytesToHex(bytes);
}
public static String toHex(byte bytes) {
return Hex.bytesToHex(bytes);
}
}