net.sf.farrago.type.runtime
Class BytePointer

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.ByteArrayInputStream
          extended by net.sf.farrago.type.runtime.BytePointer
All Implemented Interfaces:
Closeable, CharSequence, AssignableValue, DataValue
Direct Known Subclasses:
EncodedCharPointer

public class BytePointer
extends ByteArrayInputStream
implements AssignableValue, CharSequence

BytePointer is instantiated during execution to refer to a contiguous subset of a byte array. It exists to avoid the need to instantiate a new object for each variable-width value read.

NOTE: BytePointer is not declared to implement NullableValue, although it actually provides the necessary method implementations. Instead, the NullableValue interface is declared by generated subclasses representing nullable types. This allows the presence of the NullableValue interface to be used in runtime contexts where we need to determine nullability but have lost explicit type information during code generation.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/type/runtime/BytePointer.java#39 $
Author:
John V. Sichi

Field Summary
static String CONCAT_METHOD_NAME
           
static byte[] emptyBytes
          Read-only global for 0-length byte array
static String ENFORCE_PRECISION_METHOD_NAME
           
static String GET_BYTE_COUNT_METHOD_NAME
           
static String INITCAP_METHOD_NAME
           
protected  int L1
           
static String LOWER_METHOD_NAME
           
static String OVERLAY_METHOD_NAME
           
protected  byte[] ownBytes
          An allocate-on-demand array used when a new value must be created.
static String POSITION_METHOD_NAME
           
protected  int S1
          two temp variables to store the substring pointers
static String SET_POINTER_METHOD_NAME
           
static String SUBSTRING_METHOD_NAME
           
static String TRIM_METHOD_NAME
           
static String UPPER_METHOD_NAME
           
 
Fields inherited from class java.io.ByteArrayInputStream
buf, count, mark, pos
 
Fields inherited from interface net.sf.farrago.type.runtime.AssignableValue
ASSIGNMENT_METHOD_NAME
 
Constructor Summary
BytePointer()
          Creates a new BytePointer object.
 
Method Summary
protected  void allocateOwnBytes(int n)
           
protected  void allocateOwnBytesForPrecision(int n)
           
 void assignFrom(Object obj)
          Assigns value from an Object.
 long attemptFastAsciiByteToLong()
          Attempts to convert this pointer's contents from a single-byte-ASCII-encoded integer string into a long.
protected  void calcSubstringPointers(int S, int L, int LC, boolean useLength)
           
 void cast(boolean b, int precision)
           
 void cast(double d, int precision)
           
 void cast(EncodedSqlDecimal d, int precision)
           
 void cast(float f, int precision)
           
 void cast(long l, int precision)
           
private  void castDecimal(long l, int precision, int scale)
          Casts a decimal into a string.
private  void castNoChecking(double d, int precision, boolean isFloat)
           
 char charAt(int index)
           
 int compareBytes(BytePointer other)
          Implementation for Comparable.compareTo() which assumes non-null and does byte-for-byte comparison.
 void concat(BytePointer bp1, BytePointer bp2)
          Assigns this pointer to the result of concatenating two input strings.
protected  void copyFrom(BytePointer bp1)
           
 void enforceBytePrecision(int precision, boolean needPad, byte padByte)
          Pads or truncates this value according to the given precision.
protected  void finishOverlay(BytePointer bp1, BytePointer bp2, int starting)
           
 int getByteCount()
           
protected  int getByteCountForPrecision(int n)
           
protected  byte[] getBytesForString(String string)
          Gets the byte representation of a string.
protected  String getCharsetName()
           
 Object getNullableData()
           
 void initcap(BytePointer bp1)
          initcap the string.
 boolean isNull()
           
 int length()
           
 void lower(BytePointer bp1)
          lower the case for each character of the string
 void overlay(BytePointer bp1, BytePointer bp2, int starting, int length, boolean useLength)
          Assigns this value to the result of inserting bp2's value into bp1's value at a specified starting point, possibly deleting a prefix of the remainder of bp1 of a given length.
 int position(BytePointer bp1)
           
protected  int positionImpl(BytePointer bp1, int bytesPerChar)
           
protected  void setCharAt(int index, char c)
           
 void setNull(boolean isNull)
           
 void setPointer(byte[] buf, int pos, int end)
          Sets the pointer to reference a buffer.
 CharSequence subSequence(int start, int end)
           
 void substring(int starting, int length, boolean useLength)
          Reduces the value to a substring of the current value.
 String toString()
           
 void trim(int trimOrdinal, BytePointer bp1, BytePointer bp2)
           
 void upper(BytePointer bp1)
          upper the case for each character of the string
 void writeToBuffer(ByteBuffer byteBuffer)
          Writes the contents of this pointer to a ByteBuffer.
 void writeToBufferAbsolute(ByteBuffer byteBuffer, int offset)
          Writes the contents of this pointer to a ByteBuffer at a given offset without modifying the current position.
 
Methods inherited from class java.io.ByteArrayInputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENFORCE_PRECISION_METHOD_NAME

public static final String ENFORCE_PRECISION_METHOD_NAME
See Also:
Constant Field Values

SET_POINTER_METHOD_NAME

public static final String SET_POINTER_METHOD_NAME
See Also:
Constant Field Values

GET_BYTE_COUNT_METHOD_NAME

public static final String GET_BYTE_COUNT_METHOD_NAME
See Also:
Constant Field Values

SUBSTRING_METHOD_NAME

public static final String SUBSTRING_METHOD_NAME
See Also:
Constant Field Values

OVERLAY_METHOD_NAME

public static final String OVERLAY_METHOD_NAME
See Also:
Constant Field Values

INITCAP_METHOD_NAME

public static final String INITCAP_METHOD_NAME
See Also:
Constant Field Values

CONCAT_METHOD_NAME

public static final String CONCAT_METHOD_NAME
See Also:
Constant Field Values

UPPER_METHOD_NAME

public static final String UPPER_METHOD_NAME
See Also:
Constant Field Values

LOWER_METHOD_NAME

public static final String LOWER_METHOD_NAME
See Also:
Constant Field Values

TRIM_METHOD_NAME

public static final String TRIM_METHOD_NAME
See Also:
Constant Field Values

POSITION_METHOD_NAME

public static final String POSITION_METHOD_NAME
See Also:
Constant Field Values

emptyBytes

public static final byte[] emptyBytes
Read-only global for 0-length byte array


ownBytes

protected byte[] ownBytes
An allocate-on-demand array used when a new value must be created.


S1

protected int S1
two temp variables to store the substring pointers


L1

protected int L1
Constructor Detail

BytePointer

public BytePointer()
Creates a new BytePointer object.

Method Detail

setNull

public void setNull(boolean isNull)

isNull

public boolean isNull()

getNullableData

public Object getNullableData()
Specified by:
getNullableData in interface DataValue
Returns:
an Object representation of this value's data, or null if this value is null

setPointer

public void setPointer(byte[] buf,
                       int pos,
                       int end)
Sets the pointer to reference a buffer.

Parameters:
buf - the buffer to point into
pos - position in buffer to point at
end - buffer position at which valid data ends

getCharsetName

protected String getCharsetName()
Returns:
the charset used for this pointer's encoding, or BINARY if no character data is encoded

assignFrom

public void assignFrom(Object obj)
Description copied from interface: AssignableValue
Assigns value from an Object.

Specified by:
assignFrom in interface AssignableValue
Parameters:
obj - value to assign, or null to set null

enforceBytePrecision

public void enforceBytePrecision(int precision,
                                 boolean needPad,
                                 byte padByte)
Pads or truncates this value according to the given precision.

Parameters:
precision - desired precision, in characters for character data, or bytes for binary data
needPad - true if short values should be padded
padByte - byte to pad with

substring

public void substring(int starting,
                      int length,
                      boolean useLength)
Reduces the value to a substring of the current value.

Parameters:
starting - desired starting position
length - the length.
useLength - to indicate whether length parameter should be used.

overlay

public void overlay(BytePointer bp1,
                    BytePointer bp2,
                    int starting,
                    int length,
                    boolean useLength)
Assigns this value to the result of inserting bp2's value into bp1's value at a specified starting point, possibly deleting a prefix of the remainder of bp1 of a given length. Implements the SQL string OVERLAY function.

Parameters:
bp1 - string1
bp2 - string2
starting - starting point
length - length
useLength - whether to use length parameter

finishOverlay

protected void finishOverlay(BytePointer bp1,
                             BytePointer bp2,
                             int starting)

concat

public void concat(BytePointer bp1,
                   BytePointer bp2)
Assigns this pointer to the result of concatenating two input strings.

Parameters:
bp1 - string1
bp2 - string2

getByteCount

public int getByteCount()

length

public int length()
Specified by:
length in interface CharSequence

setCharAt

protected void setCharAt(int index,
                         char c)

charAt

public char charAt(int index)
Specified by:
charAt in interface CharSequence

subSequence

public CharSequence subSequence(int start,
                                int end)
Specified by:
subSequence in interface CharSequence

upper

public void upper(BytePointer bp1)
upper the case for each character of the string

Parameters:
bp1 - string1

lower

public void lower(BytePointer bp1)
lower the case for each character of the string

Parameters:
bp1 - string1

initcap

public void initcap(BytePointer bp1)
initcap the string.

Parameters:
bp1 - string1

trim

public void trim(int trimOrdinal,
                 BytePointer bp1,
                 BytePointer bp2)

position

public int position(BytePointer bp1)

positionImpl

protected int positionImpl(BytePointer bp1,
                           int bytesPerChar)

copyFrom

protected void copyFrom(BytePointer bp1)

calcSubstringPointers

protected void calcSubstringPointers(int S,
                                     int L,
                                     int LC,
                                     boolean useLength)
Reference to SQL:2003 standard:
Part 2 Section 6.29 General Rule 3

allocateOwnBytes

protected void allocateOwnBytes(int n)

allocateOwnBytesForPrecision

protected void allocateOwnBytesForPrecision(int n)

getByteCountForPrecision

protected int getByteCountForPrecision(int n)

writeToBuffer

public final void writeToBuffer(ByteBuffer byteBuffer)
Writes the contents of this pointer to a ByteBuffer.

Parameters:
byteBuffer - target

writeToBufferAbsolute

public final void writeToBufferAbsolute(ByteBuffer byteBuffer,
                                        int offset)
Writes the contents of this pointer to a ByteBuffer at a given offset without modifying the current position.

Parameters:
byteBuffer - target
offset - starting byte offset within buffer

getBytesForString

protected byte[] getBytesForString(String string)
Gets the byte representation of a string. Subclasses may override.

Parameters:
string - source
Returns:
byte representation

compareBytes

public int compareBytes(BytePointer other)
Implementation for Comparable.compareTo() which assumes non-null and does byte-for-byte comparison.

Parameters:
other - another BytePointer to be compared
Returns:
same as compareTo

toString

public String toString()
Specified by:
toString in interface CharSequence
Overrides:
toString in class Object

cast

public void cast(float f,
                 int precision)

cast

public void cast(double d,
                 int precision)

castNoChecking

private void castNoChecking(double d,
                            int precision,
                            boolean isFloat)

cast

public void cast(boolean b,
                 int precision)

cast

public void cast(long l,
                 int precision)

cast

public void cast(EncodedSqlDecimal d,
                 int precision)

attemptFastAsciiByteToLong

public long attemptFastAsciiByteToLong()
Attempts to convert this pointer's contents from a single-byte-ASCII-encoded integer string into a long.

Returns:
converted value if successful, or Long.MAX_VALUE if unsuccessful (does not necessarily indicate that cast fails, just that this fast path can't handle it, e.g. negative/decimal/floating)

castDecimal

private void castDecimal(long l,
                         int precision,
                         int scale)
Casts a decimal into a string.

Parameters:
l - long value of decimal
precision - maximum length of string
scale - scale of decimal