/***************************************************************************
 *   Copyright (C) by Fabrizio Montesi                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library 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, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package joliex.io;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import jolie.net.CommMessage;
import jolie.runtime.ByteArray;
import jolie.runtime.FaultException;
import jolie.runtime.JavaService;
import jolie.runtime.Value;

public class FileService extends JavaService
{
	private static void readBase64IntoValue( File file, Value value )
		throws IOException
	{
		FileInputStream fis = new FileInputStream( file );
		byte[] buffer = new byte[ (int)file.length() ];
		fis.read( buffer );
		fis.close();
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		value.setValue( encoder.encode( buffer ) );
	}
	
	private static void readBinaryIntoValue( File file, Value value )
		throws IOException
	{
		FileInputStream fis = new FileInputStream( file );
		byte[] buffer = new byte[ (int)file.length() ];
		fis.read( buffer );
		fis.close();
		value.setValue( new ByteArray( buffer ) );
	}
	
	private static void readTextIntoValue( File file, Value value )
		throws IOException
	{
		String separator = System.getProperty( "line.separator" );
		StringBuffer buffer = new StringBuffer();
		String line;
		BufferedReader reader = new BufferedReader( new FileReader( file ) );
		while( (line=reader.readLine()) != null ) {
			buffer.append( line );
			buffer.append( separator );
		}
		reader.close();
		value.setValue( buffer.toString() );
	}
	
	public CommMessage readFile( CommMessage message )
		throws FaultException
	{
		
		Value filenameValue = message.value().getChildren( "filename" ).first();
		if ( !filenameValue.isString() ) {
			throw new FaultException( "FileNotFound" );
		}

		Value retValue = Value.create();
		String format = message.value().getFirstChild( "format" ).strValue();

		try {
			if ( "base64".equals( format ) ) {
				readBase64IntoValue( new File( filenameValue.strValue() ), retValue );
			} else if ( "binary".equals( format ) ) {
				readBinaryIntoValue( new File( filenameValue.strValue() ), retValue );
			} else {
				readTextIntoValue( new File( filenameValue.strValue() ), retValue );
			}			
		} catch( FileNotFoundException e ) {
			throw new FaultException( "FileNotFound", e );
		} catch( IOException e ) {
			throw new FaultException( e );
		}
		return new CommMessage( message.operationName(), "/", retValue );
	}
}

@@ -1697,20 +1697,80 @@
 .io.File
-Read
+OutputStream;%0Aimport java.io.FileReader;%0Aimport java.io.FileWrit
 er;%0Aimpo
@@ -3160,19 +3160,16 @@
 tion%0A%09%7B%0A
-%09%09%0A
 %09%09Value 
@@ -3918,24 +3918,24 @@
 n( e );%0A%09%09%7D%0A
-
 %09%09return new
@@ -3993,10 +3993,811 @@
 e );%0A%09%7D%0A
+%09%0A%09public CommMessage writeFile( CommMessage request )%0A%09%09throws FaultException%0A%09%7B%0A%09%09Value filenameValue = request.value().getFirstChild( %22filename%22 );%0A%09%09if ( !filenameValue.isString() ) %7B%0A%09%09%09throw new FaultException( %22FileNotFound%22 );%0A%09%09%7D%0A%09%09Value content = request.value().getFirstChild( %22content%22 );%0A%09%09try %7B%0A%09%09%09if ( content.isByteArray() ) %7B%0A%09%09%09%09FileOutputStream os = new FileOutputStream( filenameValue.strValue() );%0A%09%09%09%09os.write( ((ByteArray)content.valueObject()).getBytes() );%0A%09%09%09%09os.flush();%0A%09%09%09%7D else %7B%0A%09%09%09%09FileWriter writer = new FileWriter( filenameValue.strValue() );%0A%09%09%09%09writer.write( content.strValue() );%0A%09%09%09%09System.out.println( content.strValue());%0A%09%09%09%09writer.flush();%0A%09%09%09%7D%0A%09%09%7D catch( IOException e ) %7B%0A%09%09%09throw new FaultException( e );%0A%09%09%7D%0A%09%09return CommMessage.createEmptyMessage();%0A%09%7D%0A
 %7D%0A

Support for writing files.