|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 NEOS-Server |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | +package org.neos.client; |
| 23 | + |
| 24 | +import java.io.BufferedReader; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.DataInputStream; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.FileNotFoundException; |
| 30 | +import java.io.FileReader; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStreamReader; |
| 33 | +import java.io.ObjectInputStream; |
| 34 | +import java.io.ObjectOutputStream; |
| 35 | +import java.io.Reader; |
| 36 | + |
| 37 | +/** |
| 38 | + * Helper class for accessing file via a generic method When Java application |
| 39 | + * accesses resource files, it can directly use file reader. However, when it is |
| 40 | + * executing as an applet, it needs to use other method. |
| 41 | + * |
| 42 | + * This class provide unified interface for accessing resource file. |
| 43 | + * |
| 44 | + * @author Thawan Kooburat |
| 45 | + * |
| 46 | + */ |
| 47 | +public class FileUtils { |
| 48 | + /** |
| 49 | + * Use this mode when running Java application directly |
| 50 | + */ |
| 51 | + public static final int APPLICATION_MODE = 0; |
| 52 | + |
| 53 | + /** |
| 54 | + * Use this mode when executing as an applet |
| 55 | + */ |
| 56 | + public static final int APPLET_MODE = 1; |
| 57 | + |
| 58 | + private static FileUtils self = null; |
| 59 | + |
| 60 | + private int mode; |
| 61 | + |
| 62 | + private FileUtils() { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Create FileUtils instance for a specified mode |
| 68 | + * |
| 69 | + * @param mode |
| 70 | + * @return |
| 71 | + */ |
| 72 | + public static FileUtils getInstance(int mode) { |
| 73 | + if (self == null) { |
| 74 | + self = new FileUtils(); |
| 75 | + self.mode = mode; |
| 76 | + } |
| 77 | + |
| 78 | + return self; |
| 79 | + } |
| 80 | + |
| 81 | + public String readFile(String fileName) { |
| 82 | + |
| 83 | + StringBuffer content = new StringBuffer(); |
| 84 | + String line; |
| 85 | + BufferedReader in; |
| 86 | + try { |
| 87 | + Reader reader; |
| 88 | + if (mode == APPLET_MODE) |
| 89 | + reader = new InputStreamReader( |
| 90 | + FileUtils.class.getResourceAsStream("/" + fileName)); |
| 91 | + else |
| 92 | + reader = new FileReader(fileName); |
| 93 | + |
| 94 | + in = new BufferedReader(reader); |
| 95 | + |
| 96 | + while ((line = in.readLine()) != null) |
| 97 | + content.append(line + "\n"); |
| 98 | + |
| 99 | + } catch (FileNotFoundException e) { |
| 100 | + // TODO Auto-generated catch block |
| 101 | + e.printStackTrace(); |
| 102 | + } catch (IOException e) { |
| 103 | + // TODO Auto-generated catch block |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + |
| 107 | + return content.toString(); |
| 108 | + } |
| 109 | + |
| 110 | + public byte[] readBinaryFile(String fileName) { |
| 111 | + |
| 112 | + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
| 113 | + |
| 114 | + byte buffer[] = new byte[1024]; |
| 115 | + |
| 116 | + int read; |
| 117 | + |
| 118 | + try { |
| 119 | + DataInputStream reader; |
| 120 | + if (mode == APPLET_MODE) |
| 121 | + reader = new DataInputStream( |
| 122 | + FileUtils.class.getResourceAsStream("/" + fileName)); |
| 123 | + else |
| 124 | + reader = new DataInputStream(new FileInputStream(fileName)); |
| 125 | + |
| 126 | + while ((read = reader.read(buffer, 0, buffer.length)) >= 0) { |
| 127 | + byteStream.write(buffer, 0, read); |
| 128 | + } |
| 129 | + |
| 130 | + } catch (FileNotFoundException e) { |
| 131 | + // TODO Auto-generated catch block |
| 132 | + e.printStackTrace(); |
| 133 | + } catch (IOException e) { |
| 134 | + // TODO Auto-generated catch block |
| 135 | + e.printStackTrace(); |
| 136 | + } |
| 137 | + |
| 138 | + return byteStream.toByteArray(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Use to clone object and its children. Useful for cloning a collection. |
| 143 | + * Standard Java clone only do shallow copy, so object in a collection is |
| 144 | + * not cloned. |
| 145 | + * |
| 146 | + * @param oldObj |
| 147 | + * @return |
| 148 | + */ |
| 149 | + static public Object deepCopy(Object oldObj) { |
| 150 | + ObjectOutputStream oos = null; |
| 151 | + ObjectInputStream ois = null; |
| 152 | + Object result = null; |
| 153 | + try { |
| 154 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A |
| 155 | + oos = new ObjectOutputStream(bos); // B |
| 156 | + // serialize and pass the object |
| 157 | + oos.writeObject(oldObj); // C |
| 158 | + oos.flush(); // D |
| 159 | + ByteArrayInputStream bin = new ByteArrayInputStream( |
| 160 | + bos.toByteArray()); // E |
| 161 | + ois = new ObjectInputStream(bin); // F |
| 162 | + // return the new object |
| 163 | + result = ois.readObject(); // G |
| 164 | + |
| 165 | + oos.close(); |
| 166 | + ois.close(); |
| 167 | + } catch (Exception e) { |
| 168 | + e.printStackTrace(); |
| 169 | + |
| 170 | + } |
| 171 | + return result; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments