org.jruby.util
Class Convert

java.lang.Object
  extended by org.jruby.util.Convert

public class Convert
extends java.lang.Object

Author:
Bill Dortch Primitive conversions adapted from java.lang.Integer/Long/Double (C) Sun Microsystems, Inc.

Constructor Summary
Convert()
           
 
Method Summary
static byte[] append(byte[] bytes, byte value)
           
static int arraySize(int x)
          Requires positive x.
static int arraySize(long x)
          Requires positive x.
static java.math.BigInteger byteArrayToBigInteger(byte[] bytes, int begin, int buflen, int base, boolean strict)
          Converts a byte array to a BigInteger value, using the specified base.
static double byteArrayToDouble(byte[] bytes, int begin, int buflen, boolean strict)
          Converts a byte array containing a RubyString representation of a double value to a double.
static long byteArrayToLong(byte[] bytes, int begin, int buflen, int base, boolean strict)
          Converts a byte array to a primitive long value, using the specified base.
static java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes)
           
static java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes, int base)
           
static java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes, int base, boolean raise)
          Converts a ByteList to a BigInteger value, using the specified base.
static double byteListToDouble(org.jruby.util.ByteList bytes)
           
static double byteListToDouble(org.jruby.util.ByteList bytes, boolean strict)
          Converts a ByteList containing a RubyString representation of a double value to a double.
static long byteListToLong(org.jruby.util.ByteList bytes)
           
static long byteListToLong(org.jruby.util.ByteList bytes, int base)
           
static long byteListToLong(org.jruby.util.ByteList bytes, int base, boolean raise)
          Converts a ByteList to a primitive long value, using the specified base.
static byte[] copy(byte[] bytes)
          Returns a copy of the array, or the array itelf if its length == 0.
static byte[] delete(byte[] bytes, int pos, boolean copy)
          Deletes the byte at the specified position, shifting all bytes to the right of it left by one byte.
static byte[] delete(byte[] bytes, int pos, int length, boolean copy)
           
static byte[] doubleToByteArray(double d)
           
static byte[] fill(byte[] bytes, int pos, int length, byte value)
          Fills the array with the specified value, starting at the specified position, for the specified length.
static void getCharBytes(int i, int index, byte[] buf)
          Places characters representing the integer i into the character array buf.
static void getCharBytes(long i, int index, byte[] buf)
          Places characters representing the integer i into the character array buf.
static void getChars(long i, int index, char[] buf)
           
static byte[] insert(byte[] bytes, int pos, byte[] value, boolean copy)
          Inserts the value array at the specified position.
static byte[] insert(byte[] bytes, int pos, byte value, boolean copy)
          Inserts a single byte at the specified position.
static org.jruby.util.ByteList intToBinaryByteList(int i)
           
static byte[] intToBinaryBytes(int i)
           
static byte[] intToByteArray(int i)
           
static byte[] intToByteArray(int i, int radix, boolean upper)
           
static org.jruby.util.ByteList intToByteList(int i)
          Returns a ByteList object representing the specified integer.
static org.jruby.util.ByteList intToByteList(int i, int radix)
          Returns a ByteList object representing the specified integer, using the specified radix.
static byte[] intToCharBytes(int i)
           
static org.jruby.util.ByteList intToHexByteList(int i)
           
static org.jruby.util.ByteList intToHexByteList(int i, boolean upper)
           
static byte[] intToHexBytes(int i)
           
static byte[] intToHexBytes(int i, boolean upper)
           
static org.jruby.util.ByteList intToOctalByteList(int i)
           
static byte[] intToOctalBytes(int i)
           
static byte[] intToRawUnsignedBytes(int i, int shift)
          Convert the integer to an unsigned number.
static byte[] intToUnsignedBytes(int i, int shift, boolean upper)
          Convert the integer to an unsigned number.
static org.jruby.util.ByteList longToBinaryByteList(long i)
           
static byte[] longToBinaryBytes(long i)
           
static byte[] longToByteArray(long i)
           
static byte[] longToByteArray(long i, int radix, boolean upper)
           
static org.jruby.util.ByteList longToByteList(long i)
          Returns a ByteList object representing the specified long.
static org.jruby.util.ByteList longToByteList(long i, int radix)
           
static byte[] longToCharBytes(long i)
           
static char[] longToChars(long i)
           
static org.jruby.util.ByteList longToHexByteList(long i)
           
static org.jruby.util.ByteList longToHexByteList(long i, boolean upper)
           
static byte[] longToHexBytes(long i)
           
static byte[] longToHexBytes(long i, boolean upper)
           
static org.jruby.util.ByteList longToOctalByteList(long i)
           
static byte[] longToOctalBytes(long i)
           
static byte[] longToRawUnsignedBytes(long i, int shift)
          Convert the long to an unsigned number.
static byte[] longToUnsignedBytes(long i, int shift, boolean upper)
          Convert the long to an unsigned number.
static int skipLeadingWhitespace(byte[] bytes)
           
static int skipTrailingWhitespace(byte[] bytes)
           
static byte[] trim(byte[] bytes)
          Trims whitespace (any bytes <= 0x20) from the beginning and end of the array.
static byte[] twosComplementToBinaryBytes(byte[] in)
           
static byte[] twosComplementToHexBytes(byte[] in, boolean upper)
           
static byte[] twosComplementToOctalBytes(byte[] in)
           
static byte[] twosComplementToUnsignedBytes(byte[] in, int shift, boolean upper)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert

public Convert()
Method Detail

intToByteList

public static final org.jruby.util.ByteList intToByteList(int i)
Returns a ByteList object representing the specified integer. The argument is converted to signed decimal representation and returned as a ByteList.

Parameters:
i - an integer to be converted.
Returns:
a ByteList representation of the argument in base 10.

intToByteArray

public static final byte[] intToByteArray(int i)

intToByteList

public static final org.jruby.util.ByteList intToByteList(int i,
                                                          int radix)
Returns a ByteList object representing the specified integer, using the specified radix. The argument is converted to signed decimal representation and returned as a ByteList.

Parameters:
i - an integer to be converted.
radix - the radix to use in the ByteList representation.
Returns:
a ByteList representation of the argument in the specified radix.

intToByteArray

public static final byte[] intToByteArray(int i,
                                          int radix,
                                          boolean upper)

longToByteList

public static final org.jruby.util.ByteList longToByteList(long i)
Returns a ByteList object representing the specified long. The argument is converted to signed decimal representation and returned as a ByteList.

Parameters:
i - a long to be converted.
Returns:
a ByteList representation of the argument in base 10.

longToByteArray

public static final byte[] longToByteArray(long i)

longToByteList

public static final org.jruby.util.ByteList longToByteList(long i,
                                                           int radix)

longToByteArray

public static final byte[] longToByteArray(long i,
                                           int radix,
                                           boolean upper)

intToCharBytes

public static final byte[] intToCharBytes(int i)

longToCharBytes

public static final byte[] longToCharBytes(long i)

longToChars

public static final char[] longToChars(long i)

getCharBytes

public static final void getCharBytes(int i,
                                      int index,
                                      byte[] buf)
Places characters representing the integer i into the character array buf. The characters are placed into the buffer backwards starting with the least significant digit at the specified index (exclusive), and working backwards from there. Will fail if i == Integer.MIN_VALUE


getCharBytes

public static final void getCharBytes(long i,
                                      int index,
                                      byte[] buf)
Places characters representing the integer i into the character array buf. The characters are placed into the buffer backwards starting with the least significant digit at the specified index (exclusive), and working backwards from there. Will fail if i == Long.MIN_VALUE


getChars

public static final void getChars(long i,
                                  int index,
                                  char[] buf)

arraySize

public static final int arraySize(long x)
Requires positive x. For negative numbers, reverse the sign before calling and add one to the result (for the '-' sign).


arraySize

public static final int arraySize(int x)
Requires positive x. For negative numbers, reverse the sign before calling and add one to the result (for the '-' sign).


intToBinaryBytes

public static final byte[] intToBinaryBytes(int i)

intToOctalBytes

public static final byte[] intToOctalBytes(int i)

intToHexBytes

public static final byte[] intToHexBytes(int i)

intToHexBytes

public static final byte[] intToHexBytes(int i,
                                         boolean upper)

intToBinaryByteList

public static final org.jruby.util.ByteList intToBinaryByteList(int i)

intToOctalByteList

public static final org.jruby.util.ByteList intToOctalByteList(int i)

intToHexByteList

public static final org.jruby.util.ByteList intToHexByteList(int i)

intToHexByteList

public static final org.jruby.util.ByteList intToHexByteList(int i,
                                                             boolean upper)

longToBinaryBytes

public static final byte[] longToBinaryBytes(long i)

longToOctalBytes

public static final byte[] longToOctalBytes(long i)

longToHexBytes

public static final byte[] longToHexBytes(long i)

longToHexBytes

public static final byte[] longToHexBytes(long i,
                                          boolean upper)

longToBinaryByteList

public static final org.jruby.util.ByteList longToBinaryByteList(long i)

longToOctalByteList

public static final org.jruby.util.ByteList longToOctalByteList(long i)

longToHexByteList

public static final org.jruby.util.ByteList longToHexByteList(long i)

longToHexByteList

public static final org.jruby.util.ByteList longToHexByteList(long i,
                                                              boolean upper)

intToRawUnsignedBytes

public static final byte[] intToRawUnsignedBytes(int i,
                                                 int shift)
Convert the integer to an unsigned number. The character bytes are right-aligned in the 32-byte result.


intToUnsignedBytes

public static final byte[] intToUnsignedBytes(int i,
                                              int shift,
                                              boolean upper)
Convert the integer to an unsigned number. The result array is sized to fit the actual character length.


longToRawUnsignedBytes

public static final byte[] longToRawUnsignedBytes(long i,
                                                  int shift)
Convert the long to an unsigned number. The character bytes are right-aligned in the 64-byte result.


longToUnsignedBytes

public static final byte[] longToUnsignedBytes(long i,
                                               int shift,
                                               boolean upper)
Convert the long to an unsigned number. The result array is sized to fit the actual character length.


byteListToLong

public static final long byteListToLong(org.jruby.util.ByteList bytes,
                                        int base,
                                        boolean raise)
Converts a ByteList to a primitive long value, using the specified base. If base is zero, defaults to 10 unless a base specifier is encountered (e.g., '0x'). Follows Ruby rules for converting Strings to Integers. Will fail with NumberFormatException if the number is too large for a long. If the raise flag is set, will also fail on certain formatting errors (zero-length array; zero-length excluding sign; no valid digits).

Parameters:
bytes -
buflen - the effective length of the array (may be less than bytes.length)
base -
raise -
Returns:
Throws:
java.lang.NumberFormatException

byteListToLong

public static final long byteListToLong(org.jruby.util.ByteList bytes,
                                        int base)

byteListToLong

public static final long byteListToLong(org.jruby.util.ByteList bytes)

byteListToBigInteger

public static final java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes,
                                                              int base,
                                                              boolean raise)
Converts a ByteList to a BigInteger value, using the specified base. If base is zero, defaults to 10 unless a base specifier is encountered (e.g., '0x'). Follows Ruby rules for converting Strings to Integers. Will fail with NumberFormatException on certain formatting errors (zero-length array; zero-length excluding sign; no valid digits).

Intended to be called after byteListToLong if that method fails.

Parameters:
bytes -
buflen - the effective length of the array (may be less than bytes.length)
base -
Returns:
Throws:
NumberFormatException, - IllegalArgumentException

byteListToBigInteger

public static final java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes,
                                                              int base)

byteListToBigInteger

public static final java.math.BigInteger byteListToBigInteger(org.jruby.util.ByteList bytes)

byteArrayToLong

public static final long byteArrayToLong(byte[] bytes,
                                         int begin,
                                         int buflen,
                                         int base,
                                         boolean strict)
Converts a byte array to a primitive long value, using the specified base. If base is zero, defaults to 10 unless a base specifier is encountered (e.g., '0x'). Follows Ruby rules for converting Strings to Integers. Will fail with NumberFormatException if the number is too large for a long. If the raise flag is set, will also fail on certain formatting errors (zero-length array; zero-length excluding sign; no valid digits).

Parameters:
bytes -
buflen - the effective length of the array (may be less than bytes.length)
base -
strict -
Returns:
Throws:
NumberFormatException, - IllegalArgumentException

byteArrayToBigInteger

public static final java.math.BigInteger byteArrayToBigInteger(byte[] bytes,
                                                               int begin,
                                                               int buflen,
                                                               int base,
                                                               boolean strict)
Converts a byte array to a BigInteger value, using the specified base. If base is zero, defaults to 10 unless a base specifier is encountered (e.g., '0x'). Follows Ruby rules for converting Strings to Integers. Will fail with NumberFormatException on certain formatting errors (zero-length array; zero-length excluding sign; no valid digits).

Intended to be called after byteArrayToLong if that method fails.

Parameters:
bytes -
buflen - the effective length of the array (may be less than bytes.length)
base -
Returns:
Throws:
NumberFormatException, - IllegalArgumentException

byteListToDouble

public static final double byteListToDouble(org.jruby.util.ByteList bytes,
                                            boolean strict)
Converts a ByteList containing a RubyString representation of a double value to a double. Equivalent to Double.parseDouble(String s), but accounts for embedded underscore characters, as permitted in Ruby strings (single underscores allowed between digits in strict mode, multiple in non-strict mode).

Parameters:
bytes - the ByteList containing the RubyString value to convert
strict - if true, strict rules (as required by Float(str)) are enforced; otherwise, the laxer rules of str.to_f are employed.
Returns:
the converted double value

byteListToDouble

public static final double byteListToDouble(org.jruby.util.ByteList bytes)

byteArrayToDouble

public static final double byteArrayToDouble(byte[] bytes,
                                             int begin,
                                             int buflen,
                                             boolean strict)
Converts a byte array containing a RubyString representation of a double value to a double. Equivalent to Double.parseDouble(String s), but accounts for embedded underscore characters, as permitted in Ruby strings (single underscores allowed between digits in strict mode, multiple in non-strict mode).

Parameters:
bytes - the array containing the RubyString value to convert
buflen - the length of the array to be used
strict - if true, strict rules (as required by Float(str)) are enforced; otherwise, the laxer rules of str.to_f are employed.
Returns:
the converted double value

doubleToByteArray

public static final byte[] doubleToByteArray(double d)

twosComplementToBinaryBytes

public static final byte[] twosComplementToBinaryBytes(byte[] in)

twosComplementToOctalBytes

public static final byte[] twosComplementToOctalBytes(byte[] in)

twosComplementToHexBytes

public static final byte[] twosComplementToHexBytes(byte[] in,
                                                    boolean upper)

twosComplementToUnsignedBytes

public static final byte[] twosComplementToUnsignedBytes(byte[] in,
                                                         int shift,
                                                         boolean upper)

skipLeadingWhitespace

public static final int skipLeadingWhitespace(byte[] bytes)

skipTrailingWhitespace

public static final int skipTrailingWhitespace(byte[] bytes)

trim

public static final byte[] trim(byte[] bytes)
Trims whitespace (any bytes <= 0x20) from the beginning and end of the array. This is equivalent to String#trim for byte arrays. If no bytes are trimmed, the original array is returned.

Parameters:
bytes - the array to be trimmed
Returns:
the trimmed array if trimming performed, otherwise the original array

delete

public static final byte[] delete(byte[] bytes,
                                  int pos,
                                  boolean copy)
Deletes the byte at the specified position, shifting all bytes to the right of it left by one byte. If the copy flag is set, a new array (one byte shorter) will be created and the original will remain unchanged; otherwise, the last byte of the array is set to zero.

Parameters:
bytes - the array to 'delete' a byte from
pos - the offset of the byte to delete
copy - if true, a new copy of the array will be created, with the original preserved

delete

public static final byte[] delete(byte[] bytes,
                                  int pos,
                                  int length,
                                  boolean copy)

insert

public static final byte[] insert(byte[] bytes,
                                  int pos,
                                  byte value,
                                  boolean copy)
Inserts a single byte at the specified position. If copy is specified, creates a new array one byte longer; otherwise shifts bytes in the existing array by one, dropping the last byte.

Parameters:
bytes -
pos -
value -
copy -
Returns:
new array if copy was specified, otherwise the original array

insert

public static final byte[] insert(byte[] bytes,
                                  int pos,
                                  byte[] value,
                                  boolean copy)
Inserts the value array at the specified position. If copy is specified, creates a new array, length == bytes.length + value.length. Otherwise, displaces bytes in the exisiting array, shifting them right by value.length and dropping value.length bytes from the end of the array.

Parameters:
bytes -
pos -
value -
copy -
Returns:
new array if copy was specified, otherwise the original array

append

public static final byte[] append(byte[] bytes,
                                  byte value)

fill

public static final byte[] fill(byte[] bytes,
                                int pos,
                                int length,
                                byte value)
Fills the array with the specified value, starting at the specified position, for the specified length. No exception is thrown if length is too big; in that case the array will be filled to the end.

Parameters:
bytes -
pos -
length -
value -
Returns:

copy

public static final byte[] copy(byte[] bytes)
Returns a copy of the array, or the array itelf if its length == 0.

Parameters:
bytes -
Returns:


Copyright © 2002-2007 JRuby Team. All Rights Reserved.