001    ///////////////////////////////////////////////////////////////////////////////
002    // Copyright (c) 2006, Frank S. Nestel, All Rights Reserved.
003    //
004    // This library is free software; you can redistribute it and/or
005    // modify it under the terms of the GNU Lesser General Public
006    // License as published by the Free Software Foundation; either
007    // version 2.1 of the License, or (at your option) any later version.
008    //
009    // This library is distributed in the hope that it will be useful,
010    // but WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    // GNU General Public License for more details.
013    //
014    // You should have received a copy of the GNU Lesser General Public
015    // License along with this program; if not, write to the Free Software
016    // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017    ///////////////////////////////////////////////////////////////////////////////
018    
019    package de.spieleck.app.turn;
020    
021    import java.io.OutputStream;
022    import java.io.InputStream;
023    import java.io.IOException;
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    
028    /**
029     * Well, have you ever seen a Java project without a util package or Util class?
030     *
031     * <p><a href="$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/Util.java $">$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/Util.java $</a></p>
032     *
033     * @author Frank S. Nestel
034     * @author $Author: nestefan $
035     * @version $Revision: 2 $ $Date: 2006-03-20 14:33:27 +0100 (Mo, 20 Mrz 2006) $ $Author: nestefan $
036     */
037    public class Util
038    {
039        public final static char SEPCHAR = ';';
040    
041        public static int parseNextInt(String line, int start)
042            throws Exception
043        {
044            int i = line.indexOf(SEPCHAR, start);
045            if ( i == -1 )
046                i = line.length();
047            return Integer.parseInt(line.substring(start,i-1));
048        }
049    
050        /** 
051         * Ensure the existence of a certain file.
052         * If it does not exist, try to copy it from the parent directory.
053         * If it is not in the parent directory, find a resource in classpath.
054         */
055        public static boolean check4file(Class clazz,
056                            File dir, String name, File dir2, String prefix)
057            throws IOException
058        {
059            File target = new File(dir, name);
060            if ( target.exists() )
061                return false;
062            OutputStream ous = new FileOutputStream(target);
063            if ( dir2 == null )
064                dir2 = dir.getParentFile();
065            File source1 = new File(dir2, name);
066            if ( source1.exists() )
067                copyStream(ous, new FileInputStream(source1));
068            else
069                copyStream(ous, clazz.getResourceAsStream(prefix+name));
070            return true;
071        }
072    
073        public static void copyStream(OutputStream os, InputStream is)
074            throws IOException
075        {
076            byte[] buffer = new byte[8192];
077            int count;
078            while ( ( count = is.read(buffer) ) >= 0 )
079            {
080                os.write(buffer, 0, count);
081            }
082            os.close();
083            is.close();
084        }
085    
086    }