1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.failurehandler;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.Reader;
  30 import java.io.Writer;
  31 import java.util.Properties;
  32 
  33 public final class Utils {
  34     private static final int BUFFER_LENGTH = 1024;
  35 
  36     public static String prependPrefix(String prefix, String name) {
  37         return  (prefix == null || prefix.isEmpty())
  38                 ? name
  39                 : (name == null || name.isEmpty())
  40                   ? prefix
  41                   : String.format("%s.%s", prefix, name);
  42     }
  43 
  44     public static void copyStream(InputStream in, OutputStream out)
  45             throws IOException {
  46         int n;
  47         byte[] buffer = new byte[BUFFER_LENGTH];
  48         while ((n = in.read(buffer)) != -1) {
  49             out.write(buffer, 0, n);
  50         }
  51         out.flush();
  52     }
  53 
  54     public static void copyStream(Reader in, Writer out)
  55             throws IOException {
  56         int n;
  57         char[] buffer = new char[BUFFER_LENGTH];
  58         while ((n = in.read(buffer)) != -1) {
  59             out.write(buffer, 0, n);
  60         }
  61         out.flush();
  62     }
  63 
  64     public static Properties getProperties(String name) {
  65         Properties properties = new Properties();
  66         String resourceName = String.format(
  67                 "/%s.%s", name.toLowerCase(), "properties");
  68         InputStream stream = Utils.class.getResourceAsStream(resourceName);
  69         if (stream == null) {
  70             throw new IllegalStateException(String.format(
  71                     "resource '%s' doesn't exist%n", resourceName));
  72         }
  73         try {
  74             try {
  75                 properties.load(stream);
  76             } finally {
  77                 stream.close();
  78             }
  79         } catch (IOException e) {
  80             throw new IllegalStateException(String.format(
  81                     "can't read resource '%s' : %s%n",
  82                     resourceName, e.getMessage()), e);
  83         }
  84         return properties;
  85     }
  86 
  87     private Utils() { }
  88 }