view src/treeVnc/DataInputStream1.java @ 51:a14076dac503

add DataInputStream2.java
author one
date Sun, 06 May 2012 20:35:51 +0900
parents 3e41bb95119b
children d8f8123dcefc
line wrap: on
line source

package treeVnc;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.AbstractSelectableChannel;

public class DataInputStream1 implements MyDataInputStream {
	ByteBuffer buf;
	
	private SocketChannel channel;
	public DataInputStream1(Socket sock) {
		channel = sock.getChannel();
	}



	public DataInputStream1() {
	}



	@Override
	public void readFully(byte[] b, int off, int len) throws IOException {
		long count  = 0;
		ByteBuffer[] bf = {ByteBuffer.wrap(b)};
		while(count < len) {
			long i = channel.read(bf, off, len);
			if(i==-1) {
				throw new IOException();
			} 
			count += i;
		}
		buf = bf[0]; 
	}

	@Override
	public int available() throws IOException {
		return buf.remaining();
	}

	@Override
	public int skipBytes(int n) throws IOException {
		return buf.position(buf.position() + n).position();
	}

	@Override
	public int readUnsignedByte() throws IOException {
		return buf.get() & 0xff;
	}

	@Override
	public int readUnsignedShort() throws IOException {
		return readUnsignedByte() * 256 + readUnsignedByte();
	}

	@Override
	public int readInt() throws IOException {
		return buf.getInt();
	}

	@Override
	public void read(byte[] headBuf) throws IOException {
		for(int i = 0 ; i < headBuf.length ; i++) {
			headBuf[i] = buf.get();
		}
	}

	@Override
	public boolean markSupported() {
		return true;
	}

	@Override
	public void mark(int i) {
		buf.position(i).mark();
	}

	@Override
	public void reset() throws IOException {
		buf.reset();
	}

}