/***************************************************************************
 *   Copyright (C) 2015 by Matthias Dieter Wallnöfer                       *
 *                                                                         *
 *   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.util;

import java.io.IOException;
import jolie.runtime.ByteArray;
import jolie.runtime.FaultException;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import jolie.runtime.embedding.RequestResponse;

public class Converter extends JavaService
{
	@RequestResponse
	public String rawToBase64( Value value )
	{
		byte[] buffer = value.byteArrayValue().getBytes();
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		return encoder.encode( buffer );
	}

	@RequestResponse
	public ByteArray base64ToRaw( Value value )
		throws FaultException
	{
		ByteArray returnValue;
		try {
			String stringValue = value.strValue();
			sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
			byte[] supportArray = decoder.decodeBuffer( stringValue );
			returnValue = new ByteArray( supportArray );
			return returnValue;
		} catch( IOException ex ) {
			throw new FaultException( ex );
		}
	}

	@RequestResponse
	public String rawToString( Value value )
		throws FaultException
	{
		byte[] buffer = value.byteArrayValue().getBytes();

		String charset = null;
		if ( value.hasChildren( "charset" ) ) {
			charset = value.getFirstChild( "charset" ).strValue();
		}
		try {
			if ( charset != null ) {
				return new String( buffer, charset );
			} else {
				return new String( buffer );
			}
		} catch ( IOException e ) {
			throw new FaultException( "IOException", e );
		}
	}

	@RequestResponse
	public ByteArray stringToRaw( Value value )
		throws FaultException
	{
		String str = value.strValue();

		String charset = null;
		if ( value.hasChildren( "charset" ) ) {
			charset = value.getFirstChild( "charset" ).strValue();
		}
		try {
			if ( charset != null ) {
				return new ByteArray( str.getBytes( charset ) );
			} else {
				return new ByteArray( str.getBytes() );
			}
		} catch ( IOException e ) {
			throw new FaultException( "IOException", e );
		}
	}
}

@@ -1577,32 +1577,57 @@
 io.IOException;%0A
+import java.util.Base64;%0A
 import jolie.run
@@ -1953,31 +1953,23 @@
 es();%0A%09%09
-sun.misc.BASE
+Base
 64
+.
 Encoder 
@@ -1978,35 +1978,26 @@
 coder = 
-new sun.misc.BASE64
+Base64.get
 Encoder(
@@ -2022,16 +2022,24 @@
 r.encode
+ToString
 ( buffer
@@ -2140,42 +2140,8 @@
 %0A%09%7B%0A
-%09%09ByteArray returnValue;%0A%09%09try %7B%0A%09
 %09%09St
@@ -2183,24 +2183,15 @@
 ;%0A%09%09
-%09sun.misc.BASE
+Base
 64
+.
 Deco
@@ -2208,27 +2208,18 @@
 r = 
-new sun.misc.BASE64
+Base64.get
 Deco
@@ -2225,17 +2225,16 @@
 oder();%0A
-%09
 %09%09byte%5B%5D
@@ -2267,14 +2267,8 @@
 code
-Buffer
 ( st
@@ -2280,25 +2280,24 @@
 alue );%0A
-%09
 %09%09return
 Value = 
@@ -2288,23 +2288,16 @@
 %09%09return
-Value =
  new Byt
@@ -2324,100 +2324,8 @@
  );%0A
-%09%09%09return returnValue;%0A%09%09%7D catch( IOException ex ) %7B%0A%09%09%09throw new FaultException( ex );%0A%09%09%7D%0A
 %09%7D%0A%0A

use base64 lib from java8