/* VMConsoleOutputStream.java -- 
   Copyright (C) 2005 Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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 General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package com.mysaifu.jvm.io;

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

/**
 * JVMのコンソールに出力するためのOutputStream
 */
public class VMConsoleOutputStream extends OutputStream {
    /**
     * バッファ内の有効長
     */
	private int length;

    /**
     * 出力用バッファ
     */
    private byte[] buffer = new byte[128];	// 要素数はかならず偶数であること

    /**
     * ネイティブの出力処理を行う
     */
    private native void writeNative(byte[] buff, int off, int len);

	/**
	 * １バイト出力する
	 */
	public void write(int b) throws IOException {
		if (length >= buffer.length) {
			// 内部バッファをフラッシュする
			flush();
		}
		buffer[length++] = (byte) b;
	}
	
	/**
	 * バッファの内容を出力する
	 */
	public void write(byte[] buff, int off, int len) throws IOException {
		if (len <= 0) {
			return;
		}
		
		// file.encoding が "UTF-16LE" 以外の場合には、UTF-16LEを表す
		// バイト配列に変換する
		if (! "UTF-16LE".equals(System.getProperty("file.encoding"))) {
			String s = new String(buff, off, len, System.getProperty("file.encoding"));
			buff = s.getBytes("UTF-16LE");
			off = 0;
			len = buff.length;
		}
		
		int remains = this.buffer.length - this.length;
		if (len > remains) {
			// バッファにすべてが収まらない場合
			if ((this.length & 0x01) != 0) {
				// 内部バッファ内のデータ数が奇数個の場合、１バイト追加する
				this.buffer[this.length++] = buff[off++];
				// 出力バイト数を１バイト減らす
				len--;
			}
			
			// 内部バッファをフラッシュする
			flush();
			
			if (len > 0 && (len & 0x01) != 0) {
				// 出力バイト数が奇数の場合、
				// 最後の１バイトを内部バッファに入れておく
				this.buffer[this.length++] = buff[off + len - 1];
				len--;
			}
			// バッファ内のデータを直接出力する
			writeNative(buff, off, len);
			
		} else {
			// バッファに収まる場合はコピーする
			System.arraycopy(buff, off, this.buffer, this.length, len);
			this.length += len;
		}
	}
	
	/**
	 * 内部バッファをフラッシュする
	 */
	public void flush() throws IOException {
		if (this.length > 0) {
			writeNative(this.buffer, 0, this.length);
			this.length = 0;
		}
	}
}
