com.exadel.flamingo.flex.messaging.util.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amf Show documentation
Show all versions of amf Show documentation
Amf deserializer used by ats-core component to share data
/*
GRANITE DATA SERVICES
Copyright (C) 2007 ADEQUATE SYSTEMS SARL
This file is part of Granite Data Services.
Granite Data Services is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
Granite Data Services is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, see .
*/
package com.exadel.flamingo.flex.messaging.util;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.*;
/**
* @author Franck WOLFF
*/
public class StringUtil {
public static final String HEX_CHARS = "0123456789ABCDEF";
public static String toHexString(Number n) {
if (n == null)
return "null";
byte[] bytes = new byte[8];
ByteBuffer bytesBuffer = ByteBuffer.wrap(bytes);
LongBuffer longBuffer = bytesBuffer.asLongBuffer();
longBuffer.put(0, n.longValue());
StringBuilder sb = new StringBuilder(16);
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i] & 0xFF;
if (b != 0 || sb.length() > 0 || i == (bytes.length - 1))
sb.append(HEX_CHARS.charAt(b >> 4)).append(HEX_CHARS.charAt(b & 0x0F));
}
return sb.toString();
}
public static String toString(Object o) {
return toString(o, -1);
}
public static String toString(Object o, int maxItems) {
if (o == null)
return "null";
if (o instanceof String)
return ("\"" + o + "\"");
if (o instanceof Character || o.getClass() == Character.TYPE)
return ("'" + o + "'");
if (o instanceof Number) {
if (o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof Long)
return o + " <0x" + toHexString((Number)o) + ">";
return String.valueOf(o);
}
if (o.getClass().isArray()) {
Class> type = o.getClass().getComponentType();
if (maxItems < 0) {
if (type.isPrimitive()) {
if (type == Byte.TYPE)
return Arrays.toString((byte[])o);
if (type == Character.TYPE)
return Arrays.toString((char[])o);
if (type == Integer.TYPE)
return Arrays.toString((int[])o);
if (type == Double.TYPE)
return Arrays.toString((double[])o);
if (type == Long.TYPE)
return Arrays.toString((long[])o);
if (type == Float.TYPE)
return Arrays.toString((float[])o);
if (type == Short.TYPE)
return Arrays.toString((short[])o);
if (type == Boolean.TYPE)
return Arrays.toString((boolean[])o);
return "[Array of unknown primitive type: " + type + "]"; // Should never append...
}
return Arrays.toString((Object[])o);
}
final int max = Math.min(maxItems, Array.getLength(o));
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy