Class to represent unsigned 64-bit numbers. : Base64 « Development Class « Java






Class to represent unsigned 64-bit numbers.

     
/*
 * D-Bus Java Implementation Copyright (c) 2005-2006 Matthew Johnson This
 * program is free software; you can redistribute it and/or modify it under the
 * terms of either the GNU Lesser General Public License Version 2 or the
 * Academic Free Licence Version 2.1. Full licence texts are included in the
 * COPYING file with this program.
 */
//package org.freedesktop.dbus;

//import static org.freedesktop.dbus.Gettext._;

import java.math.BigInteger;
import java.text.MessageFormat;

/**
 * Class to represent unsigned 64-bit numbers. Warning: Any functions which take
 * or return a <tt>long</tt> are restricted to the range of a signed 64bit
 * number. Use the BigInteger methods if you wish access to the full range.
 */
@SuppressWarnings("serial")
public class UInt64 extends Number implements Comparable<UInt64> {
  /** Maximum allowed value (when accessed as a BigInteger) */
  public static final BigInteger MAX_BIG_VALUE  = new BigInteger("18446744073709551615");
  /** Maximum allowed value (when accessed as a long) */
  public static final long       MAX_LONG_VALUE = Long.MAX_VALUE;
  /** Minimum allowed value */
  public static final long       MIN_VALUE      = 0;
  private long                   bottom;
  private long                   top;
  private BigInteger             value;

  /**
   * Create a UInt64 from a BigInteger
   * 
   * @param value
   *          Must be a valid BigInteger between MIN_VALUE&ndash;MAX_BIG_VALUE
   * @throws NumberFormatException
   *           if value is not an integer between MIN_VALUE and MAX_BIG_VALUE
   */
  public UInt64(BigInteger value) {
/*    if (null == value) { throw new NumberFormatException(MessageFormat.format(_("{0} is not between {1} and {2}."), new Object[] {
        value, MIN_VALUE, MAX_BIG_VALUE })); }
    if (0 > value.compareTo(BigInteger.ZERO)) { throw new NumberFormatException(MessageFormat.format(
        _("{0} is not between {1} and {2}."), new Object[] { value, MIN_VALUE, MAX_BIG_VALUE })); }
    if (0 < value.compareTo(MAX_BIG_VALUE)) { throw new NumberFormatException(MessageFormat.format(
        _("{0} is not between {1} and {2}."), new Object[] { value, MIN_VALUE, MAX_BIG_VALUE })); }
  */  this.value = value;
    top = this.value.shiftRight(32).and(new BigInteger("4294967295")).longValue();
    bottom = this.value.and(new BigInteger("4294967295")).longValue();
  }

  /**
   * Create a UInt64 from a long.
   * 
   * @param value
   *          Must be a valid integer within MIN_VALUE&ndash;MAX_VALUE
   * @throws NumberFormatException
   *           if value is not between MIN_VALUE and MAX_VALUE
   */
  public UInt64(long value) {
    //if ((value < MIN_VALUE) || (value > MAX_LONG_VALUE)) { throw new NumberFormatException(MessageFormat.format(
     //   _("{0} is not between {1} and {2}."), new Object[] { value, MIN_VALUE, MAX_LONG_VALUE })); }
    this.value = new BigInteger("" + value);
    top = this.value.shiftRight(32).and(new BigInteger("4294967295")).longValue();
    bottom = this.value.and(new BigInteger("4294967295")).longValue();
  }

  /**
   * Create a UInt64 from two longs.
   * 
   * @param top
   *          Most significant 4 bytes.
   * @param bottom
   *          Least significant 4 bytes.
   */
  public UInt64(long top, long bottom) {
    BigInteger a = new BigInteger("" + top);
    a = a.shiftLeft(32);
    a = a.add(new BigInteger("" + bottom));
   // if (0 > a.compareTo(BigInteger.ZERO)) { throw new NumberFormatException(MessageFormat.format(
    //    _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    //if (0 < a.compareTo(MAX_BIG_VALUE)) { throw new NumberFormatException(MessageFormat.format(
     //   _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    value = a;
    this.top = top;
    this.bottom = bottom;
  }

  /**
   * Create a UInt64 from a String.
   * 
   * @param value
   *          Must parse to a valid integer within MIN_VALUE&ndash;MAX_BIG_VALUE
   * @throws NumberFormatException
   *           if value is not an integer between MIN_VALUE and MAX_BIG_VALUE
   */
  public UInt64(String value) {
    //if (null == value) { throw new NumberFormatException(MessageFormat.format(_("{0} is not between {1} and {2}."), new Object[] {
      //  value, MIN_VALUE, MAX_BIG_VALUE })); }
    BigInteger a = new BigInteger(value);
    this.value = a;
    top = this.value.shiftRight(32).and(new BigInteger("4294967295")).longValue();
    bottom = this.value.and(new BigInteger("4294967295")).longValue();
  }

  /**
   * Least significant 4 bytes.
   */
  public long bottom() {
    return bottom;
  }

  /** The value of this as a byte. */
  @Override
  public byte byteValue() {
    return value.byteValue();
  }

  /**
   * Compare two UInt32s.
   * 
   * @return 0 if equal, -ve or +ve if they are different.
   */
  public int compareTo(UInt64 other) {
    return value.compareTo(other.value);
  }

  /** The value of this as a double. */
  @Override
  public double doubleValue() {
    return value.doubleValue();
  }

  /** Test two UInt64s for equality. */
  @Override
  public boolean equals(Object o) {
    return (o instanceof UInt64) && value.equals(((UInt64) o).value);
  }

  /** The value of this as a float. */
  @Override
  public float floatValue() {
    return value.floatValue();
  }

  @Override
  public int hashCode() {
    return value.hashCode();
  }

  /** The value of this as a int. */
  @Override
  public int intValue() {
    return value.intValue();
  }

  /** The value of this as a long. */
  @Override
  public long longValue() {
    return value.longValue();
  }

  /** The value of this as a short. */
  @Override
  public short shortValue() {
    return value.shortValue();
  }

  /**
   * Most significant 4 bytes.
   */
  public long top() {
    return top;
  }

  /** The value of this as a string. */
  @Override
  public String toString() {
    return value.toString();
  }

  /** The value of this as a BigInteger. */
  public BigInteger value() {
    return value;
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Base64 encoding/decoding.
2.Decodes Base64 data into octects
3.Implementation of MIME's Base64 encoding and decoding conversions.
4.Encode/decode for RFC 2045 Base64 as defined by RFC 2045
5.Encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein.
6.Encodes and decodes to and from Base64 notation.
7.Encodes hex octects into Base64
8.Helper class to provide Base64 encoding routines.
9.Represents a collection of 64 boolean (on/off) flags.
10.byte to be tested if it is Base64 alphabet
11.to Base64
12.One of the fastest implementation of the Base64 encoding. Jakarta and others are slower
13.array of byte to encode
14.Codes number up to radix 62
15.A Base64 Encoder/Decoder
16.A fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045
17.BASE64 encoder implementation
18.Base-64 Encoder - translates from base-64 text into binary
19.Base64 Character encoder as specified in RFC1113
20.Base64 Utils
21.Base64 encoder/decoder
22.Base64 from by Funambol, Inc.
23.Convert to hex from byte arrays and back
24.Converting hexadecimal strings
25.Encode and decode data in Base64 format as described in RFC 1521
26.Encode and decode integers, times, and internationalized strings to and from popular binary formats
27.Encoding of raw bytes to base64-encoded characters, and decoding of base64 characters to raw bytes
28.Performs Base64 encoding and/or decoding
29.Provides Base64 encoding and decoding as defined by RFC 2045
30.Provides Base64 encoding and decoding with URL and filename safe alphabet as defined by RFC 3548, section 4.
31.Provides utility methods to Base64 encode data
32.QP Decoder Stream
33.QP Encoder Stream
34.A class to decode Base64 streams and strings.
35.A class to encode Base64 streams and strings.
36.Encodes binary data to plain text as Base64
37.A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance with RFC 2045.
38.Decodes InputStreams which contain binary data in base64 form
39.Base 64 Converter
40.Base64 from org.cspoker.common.util
41.Base64 converted from code at http://iharder.sourceforge.net/base64/
42.Encodes and decodes to and from Base64 notation.
43.Simple Base64 string decoding function
44.A Base64 encoder/decoder.
45.The Base64 utility class implements Base-64 and Base-85 encoding and decoding algorithms.
46.Provides Base64 encoding and decoding
47.Code to read and write Base64-encoded text.
48.Base32 encoding/decoding class.