/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.reference.impl.external.object;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.sf.taverna.t2.reference.ExternalReferenceBuilderSPI;
import net.sf.taverna.t2.reference.ExternalReferenceConstructionException;
import net.sf.taverna.t2.reference.ReferenceContext;

/**
 * Build an InlineStringReference from an InputStream
 * 
 * @author Tom Oinn
 * 
 */
public class InlineStringReferenceBuilder implements
		ExternalReferenceBuilderSPI<InlineStringReference> {

	public InlineStringReference createReference(InputStream byteStream,
			ReferenceContext context) {
		try {
			String contents = StreamToStringConverter
					.readFile(new BufferedReader(new InputStreamReader(
							byteStream)));
			InlineStringReference ref = new InlineStringReference();
			ref.setContents(contents);
			return ref;
		} catch (IOException e) {
			throw new ExternalReferenceConstructionException(e);
		}
	}

	public float getConstructionCost() {
		return 0.1f;
	}

	public Class<InlineStringReference> getReferenceType() {
		return InlineStringReference.class;
	}

	public boolean isEnabled(ReferenceContext context) {
		return true;
	}

}

@@ -1270,16 +1270,49 @@
 mReader;
+%0Aimport java.nio.charset.Charset;
 %0A%0Aimport
@@ -1697,16 +1697,74 @@
 nce%3E %7B%0A%0A
+%09private final Charset UTF8 = Charset.forName(%22UTF-8%22);%0A%09%0A
 %09public 
@@ -1864,16 +1864,98 @@
 %09%09try %7B%0A
+%09%09%09// UTF8 is a slightly saner default than system default%0A%09%09%09// for most streams%0A
 %09%09%09Strin
@@ -2048,16 +2048,16 @@
 Reader(%0A
-
 %09%09%09%09%09%09%09b
@@ -2065,16 +2065,22 @@
 teStream
+, UTF8
 )));%0A%09%09%09

Use UTF-8 by default when converting byte arrays