Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/util/IOUtil.java


1   package com.arranger.jarl.util;
2   
3   import osbaldeston.image.BMP;
4   import sun.security.action.GetPropertyAction;
5   
6   import javax.imageio.ImageIO;
7   import java.awt.*;
8   import java.io.*;
9   import java.security.AccessController;
10  import java.util.Properties;
11  import java.util.zip.GZIPInputStream;
12  
13  import com.arranger.jarl.io.CompressedOutputStream;
14  
15  /**
16   * IOUtil handles file saving operations
17   */
18  public class IOUtil {
19  
20      protected static final int DEFAULT_BUF_SIZE = 0x2000;
21      protected static final String TEMP_FILENAME_PREFIX = "temp_";
22  
23      protected static String m_tempDirectory;
24  
25      /**
26       * Saves an image to a file
27       * The image should be of the ColorModel: BufferedImage.TYPE_INT_RGB
28       * And the file will be saved as a bmp
29       *
30       * @param image get the current image
31       * @param file the output file
32       */
33      public static void save(Image image, File file) {
34          BMP bmp = new BMP(image);
35          bmp.write(file);
36      }
37  
38      /**
39       * Writes a jpeg
40       * @param image
41       * @param file
42       */
43      public static void saveJPEG(Image image, File file) {
44          try {
45              ImageIO.write(ImageUtil.toBufferedImage(image), "jpg", file);
46          } catch (IOException e) {
47              e.printStackTrace();
48          }
49      }
50  
51      public static Image load(File file) {
52          if (file.getAbsolutePath().endsWith("jpg")) {
53              return Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath());
54          } else {
55              BMP bmp = new BMP(file);
56              return bmp.getImage();
57          }
58      }
59  
60      public static InputStream getInputStream(String location) throws IOException {
61          return new FileInputStream(location);
62      }
63  
64      public static OutputStream getOutputStream(String location) throws IOException {
65          return new FileOutputStream(location);
66      }
67  
68      public static Reader getReader(String location) throws IOException {
69          return new FileReader(location);
70      }
71  
72      public static Writer getWriter(String location) throws IOException {
73          return new FileWriter(location);
74      }
75  
76      public static Properties loadProperties(String location) throws IOException {
77          InputStream inputStream = getInputStream(location);
78          Properties properties = new Properties();
79          properties.load(inputStream);
80          inputStream.close();
81          return properties;
82      }
83  
84      public static String toString(String location) throws IOException {
85          InputStream inputStream = getInputStream(location);
86          int m_bufferSize = inputStream.available();
87          if (m_bufferSize <= 0) {
88              return "";
89          }
90  
91          byte[] charArray = new byte[m_bufferSize];
92          int bytesRead = inputStream.read(charArray);
93          if (bytesRead != m_bufferSize) {
94              throw new IOException("getContentToString: size read mismatch " +
95                  bytesRead + "/" + m_bufferSize);
96          }
97          inputStream.close();
98  
99          return new String(charArray);
100     }
101 
102     /**
103      * Get contents of file as a byte array
104      */
105     public static byte[] toByteArray(String location) throws IOException {
106 
107         byte[] bytes = null;
108         InputStream is = null;
109         try {
110             is = getInputStream(location);
111             bytes = toByteArray(is);
112         } finally {
113             if (is != null) {
114                 is.close();
115             }
116         }
117         return bytes;
118     }
119 
120     /**
121      *
122      * Like {@link #toByteArray}, but uses {@link #toCompressedByteArray}
123      */
124     public static byte[] toCompressedByteArray(String location) throws IOException {
125         byte[] bytes = null;
126         InputStream is = null;
127         try {
128             is = getInputStream(location);
129             bytes = toCompressedByteArray(is);
130         } finally {
131             if (is != null) {
132                 is.close();
133             }
134         }
135         return bytes;
136     }
137 
138     public static byte[] toByteArray(InputStream inputStream) throws IOException {
139 
140         ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
141         copyStream(inputStream, baos);
142         return baos.toByteArray();
143     }
144 
145     public static byte[] toCompressedByteArray(InputStream inputStream) throws IOException {
146 
147         ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
148         copyStream(inputStream, new CompressedOutputStream(baos));
149         return baos.toByteArray();
150     }
151 
152     public static void decompress(InputStream compressedStream, OutputStream outputStream) throws IOException {
153         copyStream(new GZIPInputStream(compressedStream), outputStream);
154     }
155 
156     /**
157      * Copies all the bits from the inputStream to the ouputStream
158      *
159      * @param inputStream the source of the bits to be copied
160      * @param outputStream the destination for the copied bits.
161      */
162     public static int copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
163         inputStream = new BufferedInputStream(inputStream);
164         outputStream = new BufferedOutputStream(outputStream);
165 
166         byte[] bbuf = getTLSByteArray();
167         int len;
168         int totalBytes = 0;
169         while ((len = inputStream.read(bbuf)) != -1) {
170             outputStream.write(bbuf, 0, len);
171             totalBytes += len;
172         }
173         outputStream.flush();
174         return totalBytes;
175     }
176 
177     /**
178      * Get the tls's byte array
179      */
180     public static byte[] getTLSByteArray() {
181         return new byte[DEFAULT_BUF_SIZE];
182     }
183 
184     public static String getTempDirectory() {
185         if (m_tempDirectory == null) {
186 
187             try {
188                 GetPropertyAction a = new GetPropertyAction("java.io.tmpdir");
189                 m_tempDirectory = ((String)AccessController.doPrivileged(a));
190                 m_tempDirectory = new File(m_tempDirectory).getCanonicalPath();
191             } catch (Exception e) {
192                 throw new IllegalStateException(e.getMessage());
193             }
194         }
195         return m_tempDirectory;
196     }
197 
198     public static String getTempFile(String extension) {
199         StringBuffer buffer = new StringBuffer();
200         File file = null;
201         do {
202             buffer.setLength(0);
203             String fileName = StringTools.appendPath(getTempDirectory(), TEMP_FILENAME_PREFIX);
204             buffer.append(fileName);
205 
206             // obtain some temp number
207             int random = (int)(Math.random() * Integer.MAX_VALUE);
208             buffer.append(String.valueOf(random));
209 
210             // append extension
211             if (!StringTools.isEmpty(extension)) {
212                 if (!extension.startsWith(".")) {
213                     buffer.append(".");
214                 }
215                 buffer.append(extension);
216             }
217 
218             file = new File(buffer.toString());
219         } while (file.exists());
220 
221         return buffer.toString();
222     }
223 
224     /**
225      * Helper function to promptForInput from <code>System.in</code>
226      */
227     public static String promptForInput(String prompt) {
228         try {
229             System.out.print(prompt);
230             return new BufferedReader(new InputStreamReader(System.in)).readLine();
231         } catch (Exception ignored) {
232         }
233         return "";
234     }
235 }