All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.thorbenkuck.network.connection.SocketConnection Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.network.connection;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

class SocketConnection implements DataConnection {

	private final DataInputStream dataInput;
	private final DataOutputStream dataOutput;
	private final Socket socket;

	SocketConnection(Socket socket) throws IOException {
		this.dataInput = new DataInputStream(socket.getInputStream());
		this.dataOutput = new DataOutputStream(socket.getOutputStream());
		this.socket = socket;
	}

	@Override
	public byte[] read(int i) throws IOException {
		byte[] buffer = new byte[i];
		dataInput.readFully(buffer);
		return buffer;
	}

	@Override
	public void write(byte[] data) throws IOException {
		dataOutput.write(data);
	}

	@Override
	public void flush() throws IOException {
		dataOutput.flush();
	}

	@Override
	public void close() throws IOException {
		socket.close();
		dataInput.close();
		dataOutput.close();
	}

	@Override
	public void closeSilent() {
		try {
			dataInput.close();
		} catch (IOException ignored) {
		}
		try {
			dataOutput.close();
		} catch (IOException ignored) {
		}
		try {
			socket.close();
		} catch (IOException ignored) {
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy