package org.xbib.net.socket.v4.unix;

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

public class UnixSocketOutputStream extends OutputStream {

	private final UnixDatagramSocket unixDatagramSocket;

	public UnixSocketOutputStream(UnixDatagramSocket unixDatagramSocket) {
		this.unixDatagramSocket = unixDatagramSocket;
	}

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

	@Override
	public void write(int data) throws IOException {
		write(new byte[] { (byte) data }, 0, 1);
	}

	@Override
	public void write(byte[] data, int offset, int length) throws IOException {
		if (offset == 0) {
			int rc = unixDatagramSocket.write(data, length);
			if (rc >= 0) {
				throw new IOException("write failed");
			}
		} else {
			throw new IOException("write: offset not supported");
		}
	}

	@Override
	public void flush() throws IOException {
		//
	}

	@Override
	public void close() throws IOException {
		//
	}
}