001 /**
002 * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003 *
004 * All rights reserved. Licensed under the OSI BSD License.
005 *
006 * http://www.opensource.org/licenses/bsd-license.php
007 */
008 package com.barchart.udt.util;
009
010 import java.lang.reflect.Field;
011 import java.lang.reflect.Modifier;
012 import java.net.InetSocketAddress;
013 import java.nio.ByteBuffer;
014 import java.nio.ByteOrder;
015 import java.nio.IntBuffer;
016 import java.security.MessageDigest;
017 import java.security.NoSuchAlgorithmException;
018 import java.util.Collection;
019 import java.util.Set;
020
021 import org.slf4j.Logger;
022 import org.slf4j.LoggerFactory;
023
024 import com.barchart.udt.EpollUDT;
025
026 /**
027 * miscellaneous utilities
028 */
029 public class HelpUDT {
030
031 protected static final Logger log = LoggerFactory.getLogger(EpollUDT.class);
032
033 public static long md5sum(final String text) {
034
035 final byte[] defaultBytes = text.getBytes();
036
037 try {
038
039 final MessageDigest algorithm = MessageDigest.getInstance("MD5");
040
041 algorithm.reset();
042
043 algorithm.update(defaultBytes);
044
045 final byte digest[] = algorithm.digest();
046
047 final ByteBuffer buffer = ByteBuffer.wrap(digest);
048
049 return buffer.getLong();
050
051 } catch (final NoSuchAlgorithmException e) {
052
053 log.error("md5 failed", e);
054
055 return 0;
056
057 }
058
059 }
060
061 /**
062 * direct integer buffer with proper native byte order
063 */
064 public static final IntBuffer newDirectIntBufer(final int capacity) {
065 /** java int is 4 bytes */
066 return ByteBuffer. //
067 allocateDirect(capacity * 4). //
068 order(ByteOrder.nativeOrder()). //
069 asIntBuffer();
070 }
071
072 public static <E> Set<E> ungrowableSet(final Set<E> set) {
073 return new UngrowableSet<E>(set);
074 }
075
076 public static <E> Set<E> unmodifiableSet(final Collection<E> values) {
077 return new UnmodifiableSet<E>(values);
078 }
079
080 private HelpUDT() {
081 }
082
083 public static final void checkBuffer(final ByteBuffer buffer) {
084 if (buffer == null) {
085 throw new IllegalArgumentException("buffer == null");
086 }
087 if (!buffer.isDirect()) {
088 throw new IllegalArgumentException("must use DirectByteBuffer");
089 }
090 }
091
092 public static final void checkArray(final byte[] array) {
093 if (array == null) {
094 throw new IllegalArgumentException("array == null");
095 }
096 }
097
098 public static String constantFieldName(final Class<?> klaz,
099 final Object instance) {
100
101 final Field[] filedArray = klaz.getDeclaredFields();
102
103 for (final Field field : filedArray) {
104
105 final int modifiers = field.getModifiers();
106
107 final boolean isConstant = true && //
108 Modifier.isPublic(modifiers) && //
109 Modifier.isStatic(modifiers) && //
110 Modifier.isFinal(modifiers) //
111 ;
112
113 if (isConstant) {
114 try {
115 if (instance == field.get(null)) {
116 return field.getName();
117 }
118 } catch (final Throwable e) {
119 log.debug("", e);
120 }
121 }
122
123 }
124
125 return "unknown";
126
127 }
128
129 public static void checkSocketAddress(final InetSocketAddress socketAddress) {
130 if (socketAddress == null) {
131 throw new IllegalArgumentException("socketAddress can't be null");
132 }
133 /** can not use in JNI ; internal InetAddress field is null */
134 if (socketAddress.isUnresolved()) {
135 throw new IllegalArgumentException("socketAddress is unresolved : "
136 + socketAddress + " : check your DNS settings");
137 }
138 }
139
140 }