Author: mturk Date: Mon Jun 22 08:59:26 2009 New Revision: 787171 URL: http://svn.apache.org/viewvc?rev=787171&view=rev Log: Add and use Utils class
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (with props) Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java?rev=787171&r1=787170&r2=787171&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor32.java Mon Jun 22 08:59:26 2009 @@ -16,6 +16,8 @@ package org.apache.commons.runtime; +import org.apache.commons.runtime.util.Utils; + /** Represents the Operating System 32-bit object descriptor. * <p> * IHANDLE always represents the posix int descriptor. @@ -74,8 +76,7 @@ public String toString() { if (PHANDLE != 0) { - String h = Integer.toHexString(PHANDLE); - return ("00000000" + h).substring(h.length()); + return Utils.hex(PHANDLE); } else if (IHANDLE >= 0) { return Integer.toString(IHANDLE); Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java?rev=787171&r1=787170&r2=787171&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Descriptor64.java Mon Jun 22 08:59:26 2009 @@ -16,6 +16,8 @@ package org.apache.commons.runtime; +import org.apache.commons.runtime.util.Utils; + /** Represents the Operating System 64-bit object descriptor. * <p> * IHANDLE always represents the posix int descriptor. @@ -74,8 +76,7 @@ public String toString() { if (PHANDLE != 0) { - String h = Long.toHexString(PHANDLE); - return ("0000000000000000" + h).substring(h.length()); + return Utils.hex(PHANDLE); } else if (IHANDLE >= 0) { return Integer.toString(IHANDLE); Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java?rev=787171&r1=787170&r2=787171&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java Mon Jun 22 08:59:26 2009 @@ -16,6 +16,8 @@ package org.apache.commons.runtime; +import org.apache.commons.runtime.util.Utils; + /** Represents the Operating System 32-bit pointer C/C++ pointer. * <p> * <b>Warning:</b><br/>Using this class improperly may crash the running JVM. @@ -138,8 +140,7 @@ public String toString() { if (POINTER != 0) { - String h = Integer.toHexString(POINTER); - return "0x" + ("00000000" + h).substring(h.length()); + return "0x" + Utils.hex(POINTER); } else { return "(nil)"; Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java?rev=787171&r1=787170&r2=787171&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java Mon Jun 22 08:59:26 2009 @@ -16,6 +16,8 @@ package org.apache.commons.runtime; +import org.apache.commons.runtime.util.Utils; + /** Represents the Operating System 32-bit pointer C/C++ pointer. * <p> * <b>Warning:</b><br/>Using this class improperly may crash the running JVM. @@ -137,8 +139,7 @@ public String toString() { if (POINTER != 0L) { - String h = Long.toHexString(POINTER); - return "0x" + ("0000000000000000" + h).substring(h.length()); + return "0x" + Utils.hex(POINTER); } else { return "(nil)"; Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java?rev=787171&view=auto ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (added) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java Mon Jun 22 08:59:26 2009 @@ -0,0 +1,124 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.runtime.util; + +import java.io.PrintStream; +import java.util.Random; + +/** + * General purpose utilities. + * + * @author Mladen Turk + */ +public final class Utils +{ + + private Utils() + { + // Private to prevent creation. + } + + private static long counter = 1024L; + private static long timer = System.currentTimeMillis(); + private static Random rnd = new Random(timer); + + /** + * Returns the unique 64 bit id. + * Values lower then 1024 are reseved for system ID's. + */ + public static long Id() + { + long id; + synchronized (Utils.class) { + id = counter++; + } + return id; + } + + public static void generateRandom(byte[] dst) + { + rnd.nextBytes(dst); + } + + public static String hex(byte val) + { + String h = Integer.toHexString(val); + if (h.length() == 1) + h = "0" + h; + return h; + } + + public static String hex(short val) + { + String h = Integer.toHexString(val); + return ("0000" + h).substring(h.length()); + } + + public static String hex(int val) + { + String h = Integer.toHexString(val); + return ("00000000" + h).substring(h.length()); + } + + public static String hex(long val) + { + String h = Long.toHexString(val); + return ("0000000000000000" + h).substring(h.length()); + } + + public static String dumpBuffer(byte[] data, int offset, int len) + { + int i; + int p = offset; + + if (offset >= data.length) + return null; + StringBuffer sb = new StringBuffer(); + sb.append(hex((short)offset)); + sb.append(": "); + for (i = 0; i < 16; i++) { + if (i < data.length) { + sb.append(hex(data[offset+i])); + if (i == 7) + sb.append("|"); + else + sb.append(" "); + } + else { + sb.append(" "); + } + } + sb.append(" | "); + for (i = offset; i < offset + 16; i++) { + if (i < data.length) { + if (!Character.isISOControl((char)data[i])) + sb.append(new Character((char)data[i])); + else + sb.append("."); + } + } + return sb.toString(); + } + + public static void dumpBuffer(byte[] data, int offset, int len, + PrintStream out) + { + out.println(dumpBuffer(data, offset, len)); + } + +} + Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java ------------------------------------------------------------------------------ svn:eol-style = native