View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.util;
22  
23  import java.io.*;
24  /***
25   * File related utilities methods
26   * @author TiongHiang Lee (thlee@onemindsoft.org)
27   * @version $Id: FileUtils.java,v 1.3 2004/09/19 20:07:04 thlee Exp $ $Name:  $
28   */
29  public final class FileUtils
30  {
31  
32      /***
33       * {@inheritDoc}
34       */
35      private FileUtils()
36      {
37      };
38  
39      /*** the forward slash * */
40      private static final String FSLASH = "/";
41  
42      /*** the back slash * */
43      private static final String BSLASH = "//";
44  
45      /*** the separator * */
46      private static final String SEPARATOR = FSLASH; //use forward slash since
47  
48      // both unix and windows jdk
49      // support it
50      /***
51       * Concat the strings to be a valid file path 
52       * @param args the string
53       * @return the file path
54       * @todo add doc to describe the behavior
55       */
56      public static String concatFilePath(String[] args)
57      {
58          StringBuffer sb = new StringBuffer(args[0]);
59          for (int i = 1; i < args.length; i++)
60          {
61              concatFilePath(sb, args[i]);
62          }
63          return sb.toString();
64      }
65  
66      //TODO: Describe behaviors
67      /***
68       * Concat filepath with the prefix and suffix
69       * @param prefix the prefix
70       * @param suffix the suffix
71       * @return the file path
72       * @todo add doc to describe the behavior
73       */
74      public static String concatFilePath(String prefix, String suffix)
75      {
76          return concatFilePath(new StringBuffer(prefix), suffix);
77      }
78  
79      /***
80       * concat a meaning file path
81       * @param prefix the prefix
82       * @param suffix the suffix
83       * @return the concat'ed file path
84       */
85      private static String concatFilePath(StringBuffer prefix, String suffix)
86      {
87          String pf = prefix.toString();
88          if (pf.endsWith(FSLASH) || pf.endsWith(BSLASH))
89          {
90              if (suffix.startsWith(FSLASH) || suffix.startsWith(BSLASH))
91              {
92                  if (suffix.length() > 1)
93                  {
94                      prefix.append(suffix.substring(1));
95                  }
96                  //else do nothing
97              } else
98              {
99                  prefix.append(suffix);
100             }
101         } else
102         {
103             if (suffix.startsWith(FSLASH) || suffix.startsWith(BSLASH))
104             {
105                 prefix.append(suffix);
106             } else
107             {
108                 prefix.append(FSLASH);
109                 prefix.append(suffix);
110             }
111         }
112         return prefix.toString();
113     }
114 
115     /***
116      * Copy the input to the output. Will not close the output after copying
117      * @param input the input stream
118      * @param output the output
119      * @param chunkSize the chunk size
120      * 
121      */
122     public static void copyStream(InputStream input, OutputStream output, int chunkSize) throws IOException
123     {
124         byte[] buffer = new byte[chunkSize];
125         int n = input.read(buffer, 0, chunkSize);
126         while (n != -1)
127         {
128             output.write(buffer, 0, n);
129             n = input.read(buffer, 0, chunkSize);
130         }
131         output.flush();
132     }
133 }