FileDocCategorySizeDatePackage
AbstractBynaryType.javaAPI DocHibernate 3.2.54979Fri Jun 09 22:24:06 BST 2006org.hibernate.type

AbstractBynaryType

public abstract class AbstractBynaryType extends MutableType implements VersionType, Comparator
Logic to bind stream of byte into a VARBINARY
author
Gavin King
author
Emmanuel Bernard

Fields Summary
Constructors Summary
Methods Summary
public intcompare(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)

		byte[] xbytes = toInternalFormat(x);
		byte[] ybytes = toInternalFormat(y);
		if ( xbytes.length < ybytes.length ) return -1;
		if ( xbytes.length > ybytes.length ) return 1;
		for ( int i=0; i<xbytes.length; i++ ) {
			if ( xbytes[i] < ybytes[i] ) return -1;
			if ( xbytes[i] > ybytes[i] ) return 1;
		}
		return 0;
	
public intcompare(java.lang.Object o1, java.lang.Object o2)

		return compare( o1, o2, null );
	
public java.lang.ObjectdeepCopyNotNull(java.lang.Object value)

		byte[] bytes = toInternalFormat(value);
		byte[] result = new byte[bytes.length];
		System.arraycopy(bytes, 0, result, 0, bytes.length);
		return toExternalFormat(result);
	
public java.lang.ObjectfromStringValue(java.lang.String xml)

		if (xml == null)
			return null;
		if (xml.length() % 2 != 0)
			throw new IllegalArgumentException("The string is not a valid xml representation of a binary content.");
		byte[] bytes = new byte[xml.length() / 2];
		for (int i = 0; i < bytes.length; i++) {
			String hexStr = xml.substring(i * 2, (i + 1) * 2);
			bytes[i] = (byte) (Integer.parseInt(hexStr, 16) + Byte.MIN_VALUE);
		}
		return toExternalFormat(bytes);
	
public java.lang.Objectget(java.sql.ResultSet rs, java.lang.String name)


		if ( Environment.useStreamsForBinary() ) {

			InputStream inputStream = rs.getBinaryStream(name);

			if (inputStream==null) return toExternalFormat( null ); // is this really necessary?

			ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048);
			byte[] buffer = new byte[2048];

			try {
				while (true) {
					int amountRead = inputStream.read(buffer);
					if (amountRead == -1) {
						break;
					}
					outputStream.write(buffer, 0, amountRead);
				}

				inputStream.close();
				outputStream.close();
			}
			catch (IOException ioe) {
				throw new HibernateException( "IOException occurred reading a binary value", ioe );
			}

			return toExternalFormat( outputStream.toByteArray() );

		}
		else {
			return toExternalFormat( rs.getBytes(name) );
		}
	
public java.util.ComparatorgetComparator()

		return this;
	
public intgetHashCode(java.lang.Object x, org.hibernate.EntityMode entityMode)

		byte[] bytes = toInternalFormat(x);
		int hashCode = 1;
		for ( int j=0; j<bytes.length; j++ ) {
			hashCode = 31 * hashCode + bytes[j];
		}
		return hashCode;
	
public abstract java.lang.StringgetName()

public booleanisEqual(java.lang.Object x, java.lang.Object y)

		return x==y || ( x!=null && y!=null && java.util.Arrays.equals( toInternalFormat(x), toInternalFormat(y) ) );
	
public java.lang.Objectnext(java.lang.Object current, org.hibernate.engine.SessionImplementor session)

		return current;
	
public java.lang.Objectseed(org.hibernate.engine.SessionImplementor session)

		return null;
	
public voidset(java.sql.PreparedStatement st, java.lang.Object value, int index)

		byte[] internalValue = toInternalFormat( value );
		if ( Environment.useStreamsForBinary() ) {
			st.setBinaryStream( index, new ByteArrayInputStream( internalValue ), internalValue.length );
		}
		else {
			st.setBytes( index, internalValue );
		}
	
public intsqlType()

		return Types.VARBINARY;
	
protected abstract java.lang.ObjecttoExternalFormat(byte[] bytes)
Convert the byte[] into the expected object type

protected abstract byte[]toInternalFormat(java.lang.Object bytes)
Convert the object into the internal byte[] representation

public java.lang.StringtoString(java.lang.Object val)

		byte[] bytes = toInternalFormat(val);
		StringBuffer buf = new StringBuffer();
		for ( int i=0; i<bytes.length; i++ ) {
			String hexStr = Integer.toHexString( bytes[i] - Byte.MIN_VALUE );
			if ( hexStr.length()==1 ) buf.append('0");
			buf.append(hexStr);
		}
		return buf.toString();