/*
 * Copyright 2012 MarkLogic Corporation
 *
 * 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 com.marklogic.client.io;

import java.io.Reader;

import com.marklogic.client.Format;
import com.marklogic.client.io.marker.JSONReadHandle;
import com.marklogic.client.io.marker.JSONWriteHandle;
import com.marklogic.client.io.marker.StructureReadHandle;
import com.marklogic.client.io.marker.StructureWriteHandle;
import com.marklogic.client.io.marker.TextReadHandle;
import com.marklogic.client.io.marker.TextWriteHandle;
import com.marklogic.client.io.marker.XMLReadHandle;
import com.marklogic.client.io.marker.XMLWriteHandle;

/**
 * A Reader Handle represents a resource as a reader for reading or writing.
 * 
 * When finished with the reader, close the reader to release the resources.
 */
public class ReaderHandle
	implements
		JSONReadHandle<Reader>, JSONWriteHandle<Reader>, 
		TextReadHandle<Reader>, TextWriteHandle<Reader>,
		XMLReadHandle<Reader>, XMLWriteHandle<Reader>,
		StructureReadHandle<Reader>, StructureWriteHandle<Reader>
{
    private Reader content;
	private Format format = Format.XML;

    public ReaderHandle() {
    }

    /**
	 * Returns a reader for a resource read from the database.
	 * 
     * When finished with the reader, close the reader to release
     * the response.
     * 
     * @return
     */
    public Reader get() {
    	return content;
    }
	public void set(Reader content) {
		this.content = content;
	}
	public ReaderHandle with(Reader content) {
		set(content);
		return this;
	}

	public Format getFormat() {
		return format;
	}
	public void setFormat(Format format) {
		this.format = format;
	}
	public ReaderHandle withFormat(Format format) {
		setFormat(format);
		return this;
	}

	public Class<Reader> receiveAs() {
		return Reader.class;
	}
	public void receiveContent(Reader content) {
		this.content = content;
	}
	public Reader sendContent() {
		if (content == null) {
			throw new IllegalStateException("No character stream to write");
		}

		return content;
	}
}

@@ -646,22 +646,245 @@
 java.io.
-Reader
+IOException;%0Aimport java.io.OutputStream;%0Aimport java.io.Reader;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.CharBuffer;%0Aimport java.nio.charset.Charset;%0Aimport java.nio.charset.CharsetEncoder;%0Aimport java.nio.charset.CoderResult
 ;%0A%0Aimpor
@@ -1562,16 +1562,36 @@
 plements
+ OutputStreamSender,
 %0A%09%09JSONR
@@ -1621,27 +1621,39 @@
 WriteHandle%3C
-Rea
+OutputStreamSen
 der%3E, %0A%09%09Tex
@@ -1681,35 +1681,47 @@
 TextWriteHandle%3C
-Rea
+OutputStreamSen
 der%3E,%0A%09%09XMLReadH
@@ -1746,27 +1746,39 @@
 WriteHandle%3C
-Rea
+OutputStreamSen
 der%3E,%0A%09%09Stru
@@ -1827,17 +1827,76 @@
 dle%3C
-Reader%3E%0A%7B
+OutputStreamSender%3E%0A%7B%0A%09final static private int BUFFER_SIZE = 1024;%0A
 %0A   
@@ -2736,16 +2736,22 @@
 c Reader
+Handle
  sendCon
@@ -2866,22 +2866,678 @@
 %09return 
-content
+this;%0A%09%7D%0A%0A%09@Override%0A%09public void write(OutputStream out) throws IOException %7B%0A%09%09Charset charset = Charset.forName(%22UTF-8%22);%0A%09%09CharsetEncoder encoder = charset.newEncoder();%0A%0A%09%09CharBuffer charBuf = CharBuffer.allocate(BUFFER_SIZE);%0A%09%09byte%5B%5D buf = new byte%5BBUFFER_SIZE * 2%5D;%0A%09%09ByteBuffer byteBuf = ByteBuffer.wrap(buf);%0A%0A%09%09while (content.read(charBuf) != -1) %7B%0A%09%09%09encoder.reset();%0A%09%09%09charBuf.flip();%0A%09%09%09byteBuf.clear();%0A%09%09%09CoderResult result = encoder.encode(charBuf, byteBuf, false);%0A%09%09%09if (result.isError()) %7B%0A%09%09%09%09throw new RuntimeException(result.toString());%0A%09%09%09%7D%0A%09%09%09byteBuf.flip();%0A%09%09%09out.write(buf, 0, byteBuf.limit());%0A%09%09%09charBuf.clear();%0A%09%09%7D%0A%0A%09%09out.flush()
 ;%0A%09%7D%0A%7D%0A

set Reader directly to output stream as UTF-8