package com.j256.ormlite.field.types;

import java.sql.SQLException;
import java.util.Arrays;

import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.support.DatabaseResults;

/**
 * Type that persists a byte[] object.
 * 
 * @author graywatson
 */
public class ByteArrayType extends BaseDataType {

	private static final ByteArrayType singleTon = new ByteArrayType();

	public static ByteArrayType getSingleton() {
		return singleTon;
	}

	private ByteArrayType() {
		super(SqlType.BYTE_ARRAY);
	}

	/**
	 * Here for others to subclass.
	 */
	protected ByteArrayType(SqlType sqlType, Class<?>[] classes) {
		super(sqlType, classes);
	}

	@Override
	public Object parseDefaultString(FieldType fieldType, String defaultStr) {
		if (defaultStr == null) {
			return null;
		} else {
			return defaultStr.getBytes();
		}
	}

	@Override
	public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException {
		return (byte[]) results.getBytes(columnPos);
	}

	@Override
	public boolean isArgumentHolderRequired() {
		return true;
	}

	@Override
	public boolean dataIsEqual(Object fieldObj1, Object fieldObj2) {
		if (fieldObj1 == null) {
			return (fieldObj2 == null);
		} else if (fieldObj2 == null) {
			return false;
		} else {
			return Arrays.equals((byte[]) fieldObj1, (byte[]) fieldObj2);
		}
	}

	@Override
	public Object resultStringToJava(FieldType fieldType, String stringValue, int columnPos) {
		return stringValue.getBytes();
	}

	@Override
	public Class<?> getPrimaryClass() {
		return byte[].class;
	}
}

@@ -28,24 +28,69 @@
 eld.types;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import java.
@@ -209,24 +209,71 @@
 ld.SqlType;%0A
+import com.j256.ormlite.misc.SqlExceptionUtil;%0A
 import com.j
@@ -355,17 +355,16 @@
 ject.%0A *
- 
 %0A * @aut
@@ -433,16 +433,93 @@
 Type %7B%0A%0A
+%09private static final String DEFAULT_STRING_BYTES_CHARSET_NAME = %22Unicode%22;%0A%0A
 %09private
@@ -932,16 +932,36 @@
 aultStr)
+ throws SQLException
  %7B%0A%09%09if 
@@ -1002,32 +1002,42 @@
 ull;%0A%09%09%7D else %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09return defaul
@@ -1050,18 +1050,183 @@
 etBytes(
-);
+getCharsetName(fieldType));%0A%09%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09%09throw SqlExceptionUtil.create(%22Could not convert default string: %22 + defaultStr, e);%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%7D%0A
@@ -1851,117 +1851,520 @@
 os) 
-%7B%0A%09%09return stringValue.getBytes();%0A%09%7D%0A%0A%09@Override%0A%09public Class%3C?%3E getPrimaryClass() %7B%0A%09%09return byte%5B%5D.class;
+throws SQLException %7B%0A%09%09try %7B%0A%09%09%09return stringValue.getBytes(getCharsetName(fieldType));%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09throw SqlExceptionUtil.create(%22Could not convert default string: %22 + stringValue, e);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public Class%3C?%3E getPrimaryClass() %7B%0A%09%09return byte%5B%5D.class;%0A%09%7D%0A%0A%09private String getCharsetName(FieldType fieldType) %7B%0A%09%09if (fieldType == null %7C%7C fieldType.getFormat() == null) %7B%0A%09%09%09return DEFAULT_STRING_BYTES_CHARSET_NAME;%0A%09%09%7D else %7B%0A%09%09%09return fieldType.getFormat();%0A%09%09%7D
 %0A%09%7D%0A

Enable users to set a character set for byte array type.