Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2010, Manuel Mausz. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - The names of the authors may not be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. import java.util.Collection;
  32. import java.util.Iterator;
  33. import java.io.ObjectOutputStream;
  34. import java.io.IOException;
  35.  
  36. /*
  37.  * Common static methods used by all applications
  38.  *
  39.  * @author Manuel Mausz (0728348)
  40.  */
  41. class Utils
  42. {
  43. static class Shutdown
  44. extends java.lang.RuntimeException
  45. {
  46. public Shutdown()
  47. {
  48. super();
  49. }
  50.  
  51. public Shutdown(String message)
  52. {
  53. super(message);
  54. }
  55. }
  56.  
  57. /*==========================================================================*/
  58.  
  59. public static <T> String join(final Collection<T> objs, final String delimiter)
  60. {
  61. if (objs == null || objs.isEmpty())
  62. return "";
  63. Iterator<T> iter = objs.iterator();
  64. StringBuffer buffer = new StringBuffer(iter.next().toString());
  65. while (iter.hasNext())
  66. buffer.append(delimiter).append(iter.next().toString());
  67. return buffer.toString();
  68. }
  69.  
  70. /*--------------------------------------------------------------------------*/
  71.  
  72. public static long parseHeaderNum(String[] args, int index)
  73. {
  74. long num = -1;
  75. try
  76. {
  77. if (args.length < index + 1)
  78. throw new NumberFormatException();
  79. num = Long.parseLong(args[index]);
  80. if (num <= 0)
  81. throw new NumberFormatException();
  82. }
  83. catch(NumberFormatException e)
  84. {
  85. System.err.println("Invalid network paket from peer");
  86. }
  87. return num;
  88. }
  89.  
  90. /*------------------------------------------------------------------------*/
  91.  
  92. public static void sendOutput(ObjectOutputStream out, String[] msgs)
  93. throws IOException
  94. {
  95. out.writeUTF("!output " + msgs.length);
  96. for(String msg : msgs)
  97. out.writeUTF(msg);
  98. out.flush();
  99. }
  100.  
  101. /*------------------------------------------------------------------------*/
  102.  
  103. public static void sendOutput(ObjectOutputStream out, String msg)
  104. throws IOException
  105. {
  106. String[] tmp = { msg };
  107. sendOutput(out, tmp);
  108. }
  109.  
  110. /*------------------------------------------------------------------------*/
  111.  
  112. public static void sendError(ObjectOutputStream out, String[] msgs)
  113. throws IOException
  114. {
  115. out.writeUTF("!error " + msgs.length);
  116. for(String msg : msgs)
  117. out.writeUTF(msg);
  118. out.flush();
  119. }
  120.  
  121. /*------------------------------------------------------------------------*/
  122.  
  123. public static void sendError(ObjectOutputStream out, String msg)
  124. throws IOException
  125. {
  126. String[] tmp = { "Error: " + msg };
  127. sendError(out, tmp);
  128. }
  129. }
  130.