/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jackrabbit.ocm.manager.atomictypeconverter.impl;

import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;

import org.apache.jackrabbit.ocm.exception.IncorrectAtomicTypeException;
import org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter;

/**
 * 
 * Byte Array Type Converter
 * 
 * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
 */
public class ByteArrayTypeConverterImpl implements AtomicTypeConverter
{
	/**
	 * 
	 * @see org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter#getValue(java.lang.Object)
	 */
	public Value getValue(ValueFactory valueFactory, Object propValue)
	{
		if (propValue == null)
		{
			return null;
		}
		String value = new String((byte[]) propValue);
		return valueFactory.createValue(value);
	}


	/**
	 * 
	 * @see org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter#getObject(javax.jcr.Value)
	 */
    public Object getObject(Value value)	
    {
		try
		{
			return value.getString().getBytes();
		}
		catch (RepositoryException e)
		{
			throw new IncorrectAtomicTypeException("Impossible to convert the value : " + value.toString() , e);
		}
	}

	/**
	 * 
	 * @see org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter#getStringValue(java.lang.Object)
	 */
	public String getXPathQueryValue(ValueFactory valueFactory, Object object)
	{
		return  "'" + object.toString() + "'";
	}
}

@@ -866,16 +866,148 @@
 .impl;%0A%0A
+import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A%0A
 import j
@@ -1778,33 +1778,52 @@
 %7D%0A%09%09
-String value = new String
+  InputStream ins = new ByteArrayInputStream
 ((by
@@ -1842,16 +1842,18 @@
 lue);%0A%09%09
+  
 return v
@@ -1876,21 +1876,19 @@
 teValue(
-value
+ins
 );%0A%09%7D%0A%0A%0A
@@ -2084,35 +2084,44 @@
 urn 
-value.getString().getBytes(
+inputStreamToBytes(value.getStream()
 );%0A%09
@@ -2132,26 +2132,16 @@
 %09catch (
-Repository
 Exceptio
@@ -2218,40 +2218,24 @@
 ert 
-the value : %22 + value.toString()
+a binary value.%22
  , e
@@ -2461,47 +2461,397 @@
 %7B%0A%09%09
-return  %22'%22 + object.toString() + %22'%22
+throw new IncorrectAtomicTypeException(%22Binary cannot be used in queries%22);%0A%09%7D%0A%09%0A%09private byte%5B%5D inputStreamToBytes(InputStream in) throws IOException %7B%0A%0A%09%09ByteArrayOutputStream out = new ByteArrayOutputStream(1024);%0A%09%09byte%5B%5D buffer = new byte%5B1024%5D;%0A%09%09int len;%0A%0A%09%09while((len = in.read(buffer)) %3E= 0)%0A%09%09out.write(buffer, 0, len);%0A%0A%09%09in.close();%0A%09%09out.close();%0A%09%09return out.toByteArray()
 ;%0A
+%09
 %09%7D%0A%7D

review the implementation. Use inputstream instead of String