/*
 * Copyright 2006-2012 The FLWOR Foundation.
 *
 * Licensed 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.zorbaxquery.api;

import java.io.IOException;
import java.io.OutputStream;

public class ZorbaOutputWrapper extends ZorbaIOStream {
  private OutputStream output;
  
  public ZorbaOutputWrapper(OutputStream aOutput) {
      output = aOutput;
  }
  
  @Override
  public void write(byte[] stream, long aLen){
        try {
            output.write(stream);
        } catch (IOException ex) {
            System.err.println("Error writing on output stream" + ex.getLocalizedMessage());
        }
      
  }
  
}

@@ -691,94 +691,528 @@
 am;%0A
-%0Apublic class ZorbaOutputWrapper extends ZorbaIOStream %7B%0A  private OutputStream output
+import java.io.OutputStreamWriter;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.CharBuffer;%0Aimport java.nio.charset.Charset;%0Aimport java.nio.charset.CharsetDecoder;%0Aimport java.nio.charset.CharsetEncoder;%0A%0Apublic class ZorbaOutputWrapper extends ZorbaIOStream %7B%0A  private OutputStream output;%0A  private static final int BUFFER_SIZE = @ZORBA_STREAM_BUFFER_SIZE@;%0A  private static CharsetDecoder decoder = Charset.forName(%22UTF-8%22).newDecoder();%0A  CharBuffer charBuffer;%0A  ByteBuffer byteBuffer;%0A  ByteBuffer byteBufferFinal
 ;%0A  
@@ -1260,24 +1260,38 @@
  aOutput) %7B%0A
+      try %7B%0A  
       output
@@ -1308,117 +1308,1074 @@
 ;%0A  
-%7D%0A  %0A  @Override%0A  public void write(byte%5B%5D stream, long aLen)%7B%0A        try %7B%0A            output.write(stream
+    %7D catch (Exception e) %7B%0A        System.out.println(%22Error creating OutputStreamWriter%22 + e.getLocalizedMessage());%0A      %7D%0A      charBuffer = CharBuffer.allocate(BUFFER_SIZE);%0A      byteBuffer = ByteBuffer.allocate(BUFFER_SIZE*2);%0A      byteBufferFinal = ByteBuffer.allocate(BUFFER_SIZE*2);%0A  %7D%0A  %0A  @Override%0A  public void write(byte%5B%5D stream, long aLen)%7B%0A        try %7B%0A            int offset = 0;%0A            int bufferLength = 0;%0A            while (aLen%3Eoffset) %7B%0A                byteBuffer.clear();%0A                bufferLength = BUFFER_SIZE;%0A                if ((offset+BUFFER_SIZE)%3EaLen) %7B%0A                    bufferLength = (int)aLen-offset;%0A                %7D%0A                offset += BUFFER_SIZE;%0A                byteBuffer.put(stream, 0, bufferLength);%0A                byteBuffer.flip();%0A                charBuffer.clear();%0A                decoder.decode(byteBuffer, charBuffer, false);%0A                charBuffer.flip();%0A            %7D%0A            ByteBuffer result = Charset.forName(%22UTF-8%22).encode(charBuffer);%0A            output.write(result.array()
 );%0A 

Fix for Linux UTF8