001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is availbale under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    
018    package sears.tools;
019    
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.FileOutputStream;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.OutputStream;
026    
027    /**
028     * Class Utils.
029     * <br><b>Summary:</b><br>
030     * This class provides some usefull subtitles and general tools.
031     */
032    public class Utils {
033        public static final String jpeg = "jpeg";
034    
035        public static final String jpg = "jpg";
036    
037        public static final String gif = "gif";
038    
039        public static final String tiff = "tiff";
040    
041        public static final String tif = "tif";
042    
043        public static final String png = "png";
044    
045        public Utils() {
046        }
047    
048        /**
049         * Method getExtension.
050         * <br><b>Summary:</b><br>
051         * return the file extension of a given file.
052         * ex: from test.jpg, return <b>String</b> jpg
053         * @param file              The <b>File</b> file to get the extension
054         * @return <b>String</b>    The file extension.
055         */
056        public static String getExtension(File file) {
057            String ext = "";
058            String s = file.getName();
059            int i = s.lastIndexOf('.');
060            if (i > 0 && i < s.length() - 1)
061                ext = s.substring(i + 1).toLowerCase();
062            return ext;
063        }
064        
065        /**
066         * Method isWindowsPlatform.
067         * <br><b>Summary:</b><br>
068         * Try to determine whether this application is running under Windows
069         * or some other platform by examing the "os.name" property.
070         *
071         * @return true if this application is running under a Windows OS
072         */
073        public static boolean isWindowsPlatform()
074        {
075            boolean isWindows = false; 
076            String os = System.getProperty("os.name");
077            if ((os != null) && (os.toLowerCase().startsWith("windows")))
078                    isWindows = true;
079    
080            return isWindows;
081        }
082        
083        /**
084         * Method copyStream.
085         * <br><b>Summary:</b><br>
086         * Copies src stream to dst stream.
087         * If the dst stream does not exist, it is created.
088         * @param src              The <b>File</b> source stream
089         * @param dst              The <b>File</b> destination stream
090         */
091       public static void copyStream(InputStream src, OutputStream dst) throws IOException {
092            // Transfer bytes from in to out
093            byte[] buf = new byte[1024];
094            int len;
095            while ((len = src.read(buf)) > 0) {
096                    dst.write(buf, 0, len);
097            }
098            src.close();
099            dst.close();
100        }
101        
102        /**
103         * Method copyFile.
104         * <br><b>Summary:</b><br>
105         * Copies src file to dst file.
106         * If the dst file does not exist, it is created.
107         * @param src              The <b>File</b> source file
108         * @param dst              The <b>File</b> destination file
109         */
110       public static void copyFile(File src, File dst) throws IOException {
111            copyStream(new FileInputStream(src),new FileOutputStream(dst));
112        }
113       
114       /**
115        * Method formatTime.
116        * <br><b>Summary:</b><br>
117        * Format time in second to "HH:MM:SS".
118        * @param timeInSecond The <b>int</b> time in second
119        * @return the formated time
120        */
121       public static String formatTime(int timeInSecond) {
122               String time = "";
123               // Calculate time
124               int hours = timeInSecond / 3600;
125               int minutes = ((timeInSecond - (hours * 3600)) / 60);
126               int seconds = (timeInSecond - (hours * 3600) - (minutes * 60));
127               time = Integer.toString(seconds);
128               if(seconds < 10)
129                       time = "0" + time;
130               time = ":" + time;
131               time = Integer.toString(minutes) + time;
132               if(minutes < 10)
133                       time = "0" + time;
134               time = ":" + time;
135               time = Integer.toString(hours) + time;
136               if(hours < 10)
137                       time = "0" + time;
138               
139               return time;
140       }
141    
142     }