commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trei...@apache.org
Subject cvs commit: jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/state StateHelper.java
Date Mon, 31 May 2004 06:56:50 GMT
treilly     2004/05/30 23:56:50

  Modified:    id/src/java/org/apache/commons/id/uuid/state
                        StateHelper.java
  Log:
  Try catch same line, StringBuffer.append IOException compat for jdk1.5
  
  Revision  Changes    Path
  1.4       +21 -31    jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/state/StateHelper.java
  
  Index: StateHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/state/StateHelper.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StateHelper.java	28 Apr 2004 03:06:21 -0000	1.3
  +++ StateHelper.java	31 May 2004 06:56:50 -0000	1.4
  @@ -16,9 +16,9 @@
   
   package org.apache.commons.id.uuid.state;
   
  +import java.io.IOException;
   import java.net.InetAddress;
   import java.net.UnknownHostException;
  -import java.security.SecureRandom;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.Random;
  @@ -28,7 +28,8 @@
   import org.apache.commons.codec.binary.Hex;
   import org.apache.commons.codec.digest.DigestUtils;
   import org.apache.commons.discovery.tools.DiscoverClass;
  -import org.apache.commons.id.uuid.UUID;
  +import org.apache.commons.id.uuid.Bytes;
  +import org.apache.commons.id.uuid.Constants;
   import org.apache.commons.id.uuid.clock.Clock;
   import org.apache.commons.id.uuid.clock.OverClockedException;
   
  @@ -39,7 +40,7 @@
    * @author Commons-Id team
    * @version $Id$
    */
  -public final class StateHelper {
  +public final class StateHelper implements Constants {
       /** The key for the System.property containing the ClockImpl String. */
       public static final String UUID_CLOCK_IMPL_PROPERTY_KEY = Clock.class.getName();
   
  @@ -110,7 +111,7 @@
        */
       public static byte[] randomNodeIdentifier() {
           //Holds the 16 byte MD5 value
  -        byte[] seed = new byte[UUID.BYTE_LENGTH];
  +        byte[] seed = new byte[UUID_BYTE_LENGTH];
           //Set the initial string buffer capacity
           //Time + Object.hashCode + HostName + Guess of all system properties
           int bufSize = (LONG_CHAR_LEN * 2) + HOSTNAME_MAX_CHAR_LEN + (2 * BUF_PAGE_SZ);
  @@ -119,8 +120,7 @@
           long time = 0;
           try {
               time = getClockImpl().getUUIDTime();
  -        }
  -        catch (OverClockedException oce) {
  +        } catch (OverClockedException oce) {
               time = System.currentTimeMillis();
           }
           randInfo.append(time);
  @@ -129,8 +129,7 @@
           try {
               InetAddress address = InetAddress.getLocalHost();
               randInfo.append(address.getHostName());
  -        }
  -        catch (UnknownHostException ukhe) {
  +        } catch (UnknownHostException ukhe) {
               randInfo.append("Host Unknown");
           }
           //Add something else "random"
  @@ -161,16 +160,10 @@
        * @return a new security quality random clock sequence.
        */
       public static short newClockSequence() {
  -        Random secureRandom;
  -        try {
  -            secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
  -        }
  -        catch (Exception e) {
  -            secureRandom = new Random();
  -        }
  +        Random random = new Random();
           byte[] bytes = new byte[BYTES_IN_SHORT];
  -        secureRandom.nextBytes(bytes);
  -        return (short) ((bytes[0] << SHIFT_BY_BYTE) | bytes[1]);
  +        random.nextBytes(bytes);
  +        return (short) (Bytes.toShort(bytes) & 0x3FFF);
       }
   
       /**
  @@ -185,8 +178,7 @@
                c = (Clock) dc.newInstance(
                Clock.class,
                Clock.DEFAULT_CLOCK_IMPL);
  -        }
  -        catch (Exception ex) {
  +        } catch (Exception ex) {
                // ignore as default implementation will be used.
           }
           return c;
  @@ -204,8 +196,7 @@
                s = (State) dc.newInstance(
                State.class,
                State.DEFAULT_STATE_IMPL);
  -        }
  -        catch (Exception ex) {
  +        } catch (Exception ex) {
                // ignore as default implementation will be used.
           }
           return s;
  @@ -225,8 +216,7 @@
           StringTokenizer tokens = new StringTokenizer(address, "-");
           if (tokens.countTokens() != MAC_ADDRESS_TOKEN_COUNT) {
               return null;
  -        }
  -        else {
  +        } else {
               for (int i = 0; i < MAC_ADDRESS_TOKEN_COUNT; i++) {
                   buf.append(tokens.nextToken());
               }
  @@ -234,8 +224,7 @@
           try {
               char[] c = buf.toString().toCharArray();
               return Hex.decodeHex(c);
  -        }
  -        catch (DecoderException de) {
  +        } catch (DecoderException de) {
               de.printStackTrace();
               return null;
           }
  @@ -249,15 +238,16 @@
        * @param address the 6 byte node id / address.
        * @return the node id /address byte array in as hexidecimal with dash
        * separating each octet.
  +     * @throws IOException an Input Output Exception.
        */
  -    public static String encodeMACAddress(byte[] address) {
  +    public static String encodeMACAddress(byte[] address) throws IOException {
          char[] chars = Hex.encodeHex(address);
          StringBuffer buf = new StringBuffer(MAC_ADDRESS_CHAR_LENGTH);
          for (int i = 0; i < chars.length; i++) {
  -            buf.append(chars[i]);
  -            if (i != chars.length - 1 && i % 2 != 0) {
  -                buf.append("-");
  -            }
  +               buf.append(chars[i]);
  +               if (i != chars.length - 1 && i % 2 != 0) {
  +                   buf.append("-");
  +               }
          }
          return buf.toString().toUpperCase();
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message