/*
 * $Id: SocketUtils.java,v 1.2 2004/11/10 15:02:47 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: SocketUtils.java,v $
 * Revision 1.2  2004/11/10 15:02:47  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.1  2003/11/28 17:13:59  koppy
 * Initiale Version
 *
 *
 */
package vkc.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import vkc.misc.VKCLogManager;

/**
 * Die Klasse liefert einige nützliche
 * Methoden bei der Programmierung mit Sockets
 * 
 * @author koppy
 */
public final class SocketUtils {

	/**
	 * Returns a buffered reader for the given socket
	 * 
	 * @param socket for this socket
	 * @return a buffered inputstream
	 * @throws IOException
	 */
	public static final BufferedReader getBufferedReader( Socket socket ) 
		throws IOException{
		
		return new BufferedReader( new InputStreamReader( socket.getInputStream() ));
	}
	
	
	/**
	 * Returns an object inputstream for the given socket
	 * @param socket
	 * @return an object inputstream
	 * @throws IOException
	 */
	public static final ObjectInputStream getObjectInputStream( Socket socket )
		throws IOException{
		
		return new ObjectInputStream( new DataInputStream(socket.getInputStream() ));		
	}
	
	
	/**
	 * Returns a buffered inputstream for the given socket
	 * @param socket
	 * @return a buffered inputstream
	 * @throws IOException
	 */
	public static final BufferedInputStream getBufferedInputStream( Socket socket )
		throws IOException{
	
		return new BufferedInputStream( socket.getInputStream() );		
	}
	
	
	/**
	 * Returns a buffered outputstream for the given socket
	 * @param socket
	 * @return a buffered outputstream
	 * @throws IOException
	 */
	public static final BufferedOutputStream getBufferedOutputStream( Socket socket )
		throws IOException{

		return new BufferedOutputStream( socket.getOutputStream() );		
	}
	
	/**
	 * Returns an object outputstream for the given socket
	 * @param socket
	 * @return an object outputstream
	 * @throws IOException
	 */
	public static final ObjectOutputStream getObjectOutputStream( Socket socket )
		throws IOException{
	
		return new ObjectOutputStream( new DataOutputStream( socket.getOutputStream() ) );		
	}
	
	
	/**
	 * Closes the given socket
	 * @param socket the socket to be closed
	 */
	public static void close( Socket socket ){
		try {
			socket.close();
		} catch (IOException e) {
			VKCLogManager.errorLog(SocketUtils.class, "", e.getMessage(),e);
		}
	}
}
