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 useful subtitles and general tools.
031     */
032    public class Utils {
033            
034            // EXTENSION CONSTANTS
035            /** Extension of jpeg file*/
036        public static final String jpeg = "jpeg";
037        /** Extension of jpg file*/
038        public static final String jpg = "jpg";
039        /** Extension of gif file*/
040        public static final String gif = "gif";
041        /** Extension of tiff file*/
042        public static final String tiff = "tiff";
043        /** Extension of tif file*/
044        public static final String tif = "tif";
045        /** Extension of png file*/
046        public static final String png = "png";
047        
048        // LINE SEPARATOR
049        /** Line separator, system dependent */
050        public static final String LINE_SEPARATOR = System.getProperty("line.separator");    
051        /** DOS line separator, useful for some DVD/DVIX player */
052        public static final String DOS_LINE_SEPARATOR = "\r\n";
053        
054        // PLATFORM RECOVERY
055        // to determine on which platform Sears is running
056        /** OS name */
057            public final static String OSname= System.getProperty("os.name");    
058            /** if is Linux OS */
059            public final static boolean isLinuxPlatform = OSname.toLowerCase().startsWith("linux");
060            /** if is Mac OS */
061            public final static boolean isMacPlatform = OSname.toLowerCase().startsWith("mac");
062            /** if is Windows OS */
063            public final static boolean isWindowsPlatform = OSname.toLowerCase().startsWith("windows");    
064    
065            /**
066             * Default constructor
067             */
068        public Utils() {
069        }
070    
071        /**
072         * Method getExtension.
073         * <br><b>Summary:</b><br>
074         * return the file extension of a given file.
075         * ex: from test.jpg, return <b>String</b> jpg
076         * @param file              The <b>File</b> file to get the extension
077         * @return <b>String</b>    The file extension.
078         */
079        public static String getExtension(File file) {
080            String ext = "";
081            String s = file.getName();
082            int i = s.lastIndexOf('.');
083            if (i > 0 && i < s.length() - 1)
084                ext = s.substring(i + 1).toLowerCase();
085            return ext;
086        }
087        
088        // Note: could be deleted ...
089        /**
090         * Method isWindowsPlatform.
091         * <br><b>Summary:</b><br>
092         * Try to determine whether this application is running under Windows
093         * or some other platform by examing the "os.name" property.
094         *
095         * @return true if this application is running under a Windows OS
096         */
097        public static boolean isWindowsPlatform()
098        {
099            boolean isWindows = false; 
100            String os = System.getProperty("os.name");
101            if ((os != null) && (os.toLowerCase().startsWith("windows")))
102                    isWindows = true;
103    
104            return isWindows;
105        }
106        
107        /**
108         * Method copyStream.
109         * <br><b>Summary:</b><br>
110         * Copies src stream to dst stream.
111         * If the dst stream does not exist, it is created.
112         * @param src              The <b>File</b> source stream
113         * @param dst              The <b>File</b> destination stream
114         */
115       public static void copyStream(InputStream src, OutputStream dst) throws IOException {
116            // Transfer bytes from in to out
117            byte[] buf = new byte[1024];
118            int len;
119            while ((len = src.read(buf)) > 0) {
120                    dst.write(buf, 0, len);
121            }
122            src.close();
123            dst.close();
124        }
125        
126        /**
127         * Method copyFile.
128         * <br><b>Summary:</b><br>
129         * Copies src file to dst file.
130         * If the dst file does not exist, it is created.
131         * @param src              The <b>File</b> source file
132         * @param dst              The <b>File</b> destination file
133         */
134       public static void copyFile(File src, File dst) throws IOException {
135            copyStream(new FileInputStream(src),new FileOutputStream(dst));
136        }
137       
138       /**
139        * Method formatTime.
140        * <br><b>Summary:</b><br>
141        * Format time in second to "HH:MM:SS".
142        * @param timeInSecond The <b>int</b> time in second
143        * @return the formated time
144        */
145       public static String formatTime(int timeInSecond) {
146               String time = "";
147               // Calculate time
148               int hours = timeInSecond / 3600;
149               int minutes = ((timeInSecond - (hours * 3600)) / 60);
150               int seconds = (timeInSecond - (hours * 3600) - (minutes * 60));
151               time = Integer.toString(seconds);
152               if(seconds < 10)
153                       time = "0" + time;
154               time = ":" + time;
155               time = Integer.toString(minutes) + time;
156               if(minutes < 10)
157                       time = "0" + time;
158               time = ":" + time;
159               time = Integer.toString(hours) + time;
160               if(hours < 10)
161                       time = "0" + time;
162               
163               return time;
164       }
165    
166     }