src/core/net/sf/basedb/util/SocketUtil.java

Code
Comments
Other
Rev Date Author Line
2634 12 Sep 06 nicklas 1 /**
2634 12 Sep 06 nicklas 2   $Id$
2634 12 Sep 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2634 12 Sep 06 nicklas 5
2634 12 Sep 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2634 12 Sep 06 nicklas 7   Available at http://base.thep.lu.se/
2634 12 Sep 06 nicklas 8
2634 12 Sep 06 nicklas 9   BASE is free software; you can redistribute it and/or
2634 12 Sep 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2634 12 Sep 06 nicklas 12   of the License, or (at your option) any later version.
2634 12 Sep 06 nicklas 13
2634 12 Sep 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2634 12 Sep 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2634 12 Sep 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2634 12 Sep 06 nicklas 17   GNU General Public License for more details.
2634 12 Sep 06 nicklas 18
2634 12 Sep 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2634 12 Sep 06 nicklas 21 */
2634 12 Sep 06 nicklas 22 package net.sf.basedb.util;
2634 12 Sep 06 nicklas 23
2634 12 Sep 06 nicklas 24 import java.io.BufferedReader;
2634 12 Sep 06 nicklas 25 import java.io.IOException;
2634 12 Sep 06 nicklas 26 import java.io.InputStream;
2634 12 Sep 06 nicklas 27 import java.io.InputStreamReader;
2634 12 Sep 06 nicklas 28 import java.io.PrintWriter;
2634 12 Sep 06 nicklas 29 import java.io.Reader;
2634 12 Sep 06 nicklas 30 import java.io.Writer;
2634 12 Sep 06 nicklas 31 import java.net.InetAddress;
2634 12 Sep 06 nicklas 32 import java.net.ServerSocket;
2634 12 Sep 06 nicklas 33 import java.net.Socket;
2641 14 Sep 06 nicklas 34 import java.net.UnknownHostException;
2634 12 Sep 06 nicklas 35 import java.nio.channels.ServerSocketChannel;
2634 12 Sep 06 nicklas 36
2648 19 Sep 06 nicklas 37
2634 12 Sep 06 nicklas 38 /**
2634 12 Sep 06 nicklas 39   This class contains some useful methods when working with sockets. Mainly
2634 12 Sep 06 nicklas 40   for easy reading and writing to/from the sockets, and also for closing
2634 12 Sep 06 nicklas 41   sockets without throwing exceptions.
2634 12 Sep 06 nicklas 42
2634 12 Sep 06 nicklas 43   @author nicklas
2634 12 Sep 06 nicklas 44   @version 2.0
2634 12 Sep 06 nicklas 45   @base.modified $Date$
2634 12 Sep 06 nicklas 46 */
2634 12 Sep 06 nicklas 47 public class SocketUtil
2634 12 Sep 06 nicklas 48 {
2634 12 Sep 06 nicklas 49
2634 12 Sep 06 nicklas 50   private static InetAddress LOCAL_HOST = null;
2634 12 Sep 06 nicklas 51   static
2634 12 Sep 06 nicklas 52   {
2634 12 Sep 06 nicklas 53     try
2634 12 Sep 06 nicklas 54     {
2634 12 Sep 06 nicklas 55       LOCAL_HOST = InetAddress.getLocalHost();
2634 12 Sep 06 nicklas 56     }
2634 12 Sep 06 nicklas 57     catch (Throwable t)
2634 12 Sep 06 nicklas 58     {
2634 12 Sep 06 nicklas 59       t.printStackTrace();
2634 12 Sep 06 nicklas 60     }
2634 12 Sep 06 nicklas 61   }
2634 12 Sep 06 nicklas 62   
2634 12 Sep 06 nicklas 63   /**
2634 12 Sep 06 nicklas 64     Send output to a socket.
2634 12 Sep 06 nicklas 65     @param socket The socket to send to
2634 12 Sep 06 nicklas 66     @param output The output to send
2634 12 Sep 06 nicklas 67     @param shutdownOutput TRUE to close the output stream, FALSE to leave it open
2634 12 Sep 06 nicklas 68     @throws IOException If there is an error
2634 12 Sep 06 nicklas 69    */
2634 12 Sep 06 nicklas 70   public static void send(Socket socket, String output, boolean shutdownOutput)
2634 12 Sep 06 nicklas 71     throws IOException
2634 12 Sep 06 nicklas 72   {
2634 12 Sep 06 nicklas 73     PrintWriter out = new PrintWriter(socket.getOutputStream());
2634 12 Sep 06 nicklas 74     out.println(output);
2634 12 Sep 06 nicklas 75     out.flush();
2634 12 Sep 06 nicklas 76     if (shutdownOutput) socket.shutdownOutput();
2634 12 Sep 06 nicklas 77   }
2634 12 Sep 06 nicklas 78
2634 12 Sep 06 nicklas 79   /**
2634 12 Sep 06 nicklas 80     Read input from a socket.
2634 12 Sep 06 nicklas 81     @param socket The socket to read from
2634 12 Sep 06 nicklas 82     @param shutdownInput If the input stream should be closed or not
2634 12 Sep 06 nicklas 83     @return The data that was read
2634 12 Sep 06 nicklas 84     @throws IOException If there is an error
2634 12 Sep 06 nicklas 85   */
2634 12 Sep 06 nicklas 86   public static String read(Socket socket, boolean shutdownInput)
2634 12 Sep 06 nicklas 87     throws IOException
2634 12 Sep 06 nicklas 88   {
2634 12 Sep 06 nicklas 89     StringBuilder input = new StringBuilder();
2634 12 Sep 06 nicklas 90     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
2634 12 Sep 06 nicklas 91     boolean more = true;
2634 12 Sep 06 nicklas 92     while (more)
2634 12 Sep 06 nicklas 93     {
2634 12 Sep 06 nicklas 94       String line = in.readLine();
2634 12 Sep 06 nicklas 95       if (line == null)
2634 12 Sep 06 nicklas 96       {
2634 12 Sep 06 nicklas 97         more = false;
2634 12 Sep 06 nicklas 98       }
2634 12 Sep 06 nicklas 99       else
2634 12 Sep 06 nicklas 100       {
2634 12 Sep 06 nicklas 101         if (input.length() > 0) input.append("\n");
2634 12 Sep 06 nicklas 102         input.append(line);
2634 12 Sep 06 nicklas 103       }
2634 12 Sep 06 nicklas 104     }
2634 12 Sep 06 nicklas 105     if (shutdownInput) socket.shutdownInput();
2634 12 Sep 06 nicklas 106     return input.toString();
2634 12 Sep 06 nicklas 107   }
2634 12 Sep 06 nicklas 108   
2634 12 Sep 06 nicklas 109   /**
2634 12 Sep 06 nicklas 110     Close a socket without throwing any exceptions.
2634 12 Sep 06 nicklas 111     @param socket The socket to close
2634 12 Sep 06 nicklas 112   */
2634 12 Sep 06 nicklas 113   public static void close(Socket socket)
2634 12 Sep 06 nicklas 114   {
2634 12 Sep 06 nicklas 115     if (socket == null) return;
2634 12 Sep 06 nicklas 116     try
2634 12 Sep 06 nicklas 117     {
2634 12 Sep 06 nicklas 118       socket.close();
2634 12 Sep 06 nicklas 119     }
2634 12 Sep 06 nicklas 120     catch (IOException ex)
2634 12 Sep 06 nicklas 121     {}
2634 12 Sep 06 nicklas 122   }
2634 12 Sep 06 nicklas 123   
2634 12 Sep 06 nicklas 124   /**
2634 12 Sep 06 nicklas 125     Close a server socket channel without throwing any exceptions.
2634 12 Sep 06 nicklas 126     @param socket The socket to close
2634 12 Sep 06 nicklas 127   */
2634 12 Sep 06 nicklas 128   public static void close(ServerSocketChannel socket)
2634 12 Sep 06 nicklas 129   {
2634 12 Sep 06 nicklas 130     if (socket == null) return;
2634 12 Sep 06 nicklas 131     try
2634 12 Sep 06 nicklas 132     {
2634 12 Sep 06 nicklas 133       socket.close();
2634 12 Sep 06 nicklas 134     }
2634 12 Sep 06 nicklas 135     catch (IOException ex)
2634 12 Sep 06 nicklas 136     {}
2634 12 Sep 06 nicklas 137   }  
2634 12 Sep 06 nicklas 138   
2634 12 Sep 06 nicklas 139   /**
2634 12 Sep 06 nicklas 140     Close a server socket without throwing any exceptions.
2634 12 Sep 06 nicklas 141     @param socket The socket to close
2634 12 Sep 06 nicklas 142   */
2634 12 Sep 06 nicklas 143   public static void close(ServerSocket socket)
2634 12 Sep 06 nicklas 144   {
2634 12 Sep 06 nicklas 145     if (socket == null) return;
2634 12 Sep 06 nicklas 146     try
2634 12 Sep 06 nicklas 147     {
2634 12 Sep 06 nicklas 148       socket.close();
2634 12 Sep 06 nicklas 149     }
2634 12 Sep 06 nicklas 150     catch (IOException ex)
2634 12 Sep 06 nicklas 151     {}
2634 12 Sep 06 nicklas 152   }  
2634 12 Sep 06 nicklas 153
2634 12 Sep 06 nicklas 154   
2634 12 Sep 06 nicklas 155   /**
2634 12 Sep 06 nicklas 156     Close a <code>Writer</code> without throwing any exceptions.
2634 12 Sep 06 nicklas 157     @param out The writer to close
2634 12 Sep 06 nicklas 158   */
2634 12 Sep 06 nicklas 159   public static void close(Writer out)
2634 12 Sep 06 nicklas 160   {
2634 12 Sep 06 nicklas 161     if (out == null) return;
2634 12 Sep 06 nicklas 162     try
2634 12 Sep 06 nicklas 163     {
2634 12 Sep 06 nicklas 164       out.close();
2634 12 Sep 06 nicklas 165     }
2634 12 Sep 06 nicklas 166     catch (IOException ex)
2634 12 Sep 06 nicklas 167     {}
2634 12 Sep 06 nicklas 168   }
2634 12 Sep 06 nicklas 169   
2634 12 Sep 06 nicklas 170   /**
2634 12 Sep 06 nicklas 171     Close a <code>Reader</code> without throwing any exceptions.
2634 12 Sep 06 nicklas 172     @param in The reader to close
2634 12 Sep 06 nicklas 173   */
2634 12 Sep 06 nicklas 174   public static void close(Reader in)
2634 12 Sep 06 nicklas 175   {
2634 12 Sep 06 nicklas 176     if (in == null) return;
2634 12 Sep 06 nicklas 177     try
2634 12 Sep 06 nicklas 178     {
2634 12 Sep 06 nicklas 179       in.close();
2634 12 Sep 06 nicklas 180     }
2634 12 Sep 06 nicklas 181     catch (IOException ex)
2634 12 Sep 06 nicklas 182     {}
2634 12 Sep 06 nicklas 183   }
2634 12 Sep 06 nicklas 184   
2634 12 Sep 06 nicklas 185   /**
2634 12 Sep 06 nicklas 186     Close an <code>InputStream</code> without throwing any exceptions.
2634 12 Sep 06 nicklas 187     @param in The stream to close
2634 12 Sep 06 nicklas 188   */
2634 12 Sep 06 nicklas 189   public static void close(InputStream in)
2634 12 Sep 06 nicklas 190   {
2634 12 Sep 06 nicklas 191     if (in == null) return;
2634 12 Sep 06 nicklas 192     try
2634 12 Sep 06 nicklas 193     {
2634 12 Sep 06 nicklas 194       in.close();
2634 12 Sep 06 nicklas 195     }
2634 12 Sep 06 nicklas 196     catch (IOException ex)
2634 12 Sep 06 nicklas 197     {}
2634 12 Sep 06 nicklas 198   }
2634 12 Sep 06 nicklas 199   
2634 12 Sep 06 nicklas 200   /**
2634 12 Sep 06 nicklas 201     Check if the specfied address is the local host or not.
2634 12 Sep 06 nicklas 202     @param address The address to check
2634 12 Sep 06 nicklas 203     @return TRUE if the address represents the local host, FALSE otherwise
2634 12 Sep 06 nicklas 204    */
2634 12 Sep 06 nicklas 205   public static boolean isLocalHost(InetAddress address)
2634 12 Sep 06 nicklas 206   {
5804 12 Oct 11 nicklas 207     return address.equals(LOCAL_HOST) || address.isLoopbackAddress();
2634 12 Sep 06 nicklas 208   }
2634 12 Sep 06 nicklas 209     
2634 12 Sep 06 nicklas 210   /**
2641 14 Sep 06 nicklas 211     Get the local address of the local host.
2634 12 Sep 06 nicklas 212   */
2634 12 Sep 06 nicklas 213   public static InetAddress getLocalHost()
2634 12 Sep 06 nicklas 214   {
2634 12 Sep 06 nicklas 215     return LOCAL_HOST;
2634 12 Sep 06 nicklas 216   }
2634 12 Sep 06 nicklas 217   
2641 14 Sep 06 nicklas 218   /**
2641 14 Sep 06 nicklas 219     Get the external address of the local host.
2641 14 Sep 06 nicklas 220   */
2641 14 Sep 06 nicklas 221   public static InetAddress getPublicLocalHost()
2641 14 Sep 06 nicklas 222   {
2641 14 Sep 06 nicklas 223     String hostName = LOCAL_HOST.getCanonicalHostName();
2641 14 Sep 06 nicklas 224     InetAddress adress = LOCAL_HOST;
2641 14 Sep 06 nicklas 225     try
2641 14 Sep 06 nicklas 226     {
2641 14 Sep 06 nicklas 227       adress = InetAddress.getByName(hostName);
2641 14 Sep 06 nicklas 228     }
2641 14 Sep 06 nicklas 229     catch (UnknownHostException ex)
2641 14 Sep 06 nicklas 230     {
2641 14 Sep 06 nicklas 231       ex.printStackTrace();
2641 14 Sep 06 nicklas 232     }
2641 14 Sep 06 nicklas 233     return adress;
2641 14 Sep 06 nicklas 234   }
2641 14 Sep 06 nicklas 235   
2641 14 Sep 06 nicklas 236   
2634 12 Sep 06 nicklas 237 }