channeloutputstream.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 66 行

JAVA
66
字号

package org.jnode.util;

import java.io.OutputStream;
import java.io.IOException;
import java.nio.ByteBuffer; 
import java.nio.channels.WritableByteChannel; 

/**
 * This is a stream wrapper for a WritableByteChannel. This stream
 * buffers the data internally. The buffer contents are written to
 * the channel with the flush() method.
 * 
 * Currently throws an IOException if not all bytes can be written.
 */
public class ChannelOutputStream extends OutputStream
{
	private WritableByteChannel channel;
	private ByteBuffer buffer; 
	
	public ChannelOutputStream (WritableByteChannel c, int bufsize)
	{
		channel = c;
		buffer = ByteBuffer.allocateDirect (bufsize);
	}
	
	public void write (int b) throws IOException
	{
		buffer.put ((byte)b);
		if (!buffer.hasRemaining ())
			flush ();
	}

	public void write (byte[] b) throws IOException, NullPointerException
	{
		flush ();
		out (ByteBuffer.wrap (b));
	}

	public void write (byte[] b, int off, int len)
	  throws IOException, NullPointerException, IndexOutOfBoundsException
	{
		flush ();
		out (ByteBuffer.wrap (b, off, len));
	}

	public void flush () throws IOException
	{
		buffer.flip ();
		out (buffer);
		buffer.clear ();
	}

	public void close () throws IOException
	{
		channel.close ();
	}
	
	private void out (ByteBuffer b) throws IOException
	{
		int n = b.remaining ();
		if (channel.write (b) != n)
			throw new IOException ("could not write all bytes"); 
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?