Debian Codegraph @

src/org/stathissideris/ascii2image/core/FileUtils.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * DiTAA - Diagrams Through Ascii Art
3 *
4 * Copyright (C) 2004 Efstathios Sideris
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21 package org.stathissideris.ascii2image.core;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 /**
29 *
30 * @author Efstathios Sideris
31 */
32 public class FileUtils {
33
34 //private static final
35
36 public static String makeTargetPathname(String sourcePathname, String extension, boolean overwrite){
37 return makeTargetPathname(sourcePathname, extension, "", overwrite);
38 }
39
40 public static String makeTargetPathname(String sourcePathname, String extension, String postfix, boolean overwrite){
41 File sourceFile =
42 new File(sourcePathname);
43
44 String path = "";
45 if(sourceFile.getParentFile() != null){
46 path = sourceFile.getParentFile().getAbsolutePath();
47 if(!path.endsWith(File.separator)) path += File.separator;
48 }
49 String baseName = getBaseName(sourceFile.getName());
50
51 String targetName = path + baseName + postfix + "." + extension;
52 if(new File(targetName).exists() && !overwrite)
53 targetName = makeAlternativePathname(targetName);
54 return targetName;
55 }
56
57 public static String makeAlternativePathname(String pathName){
58 int limit = 100;
59
60 for(int i = 2; i <= limit; i++){
61 String alternative = getBaseName(pathName)+"_"+i;
62 String extension = getExtension(pathName);
63 if(extension != null) alternative += "."+extension;
64 if(!(new File(alternative).exists())) return alternative;
65 }
66 return null;
67 }
68
69 public static String getExtension(String pathName){
70 if(pathName.lastIndexOf('.') == -1) return null;
71 return pathName.substring(pathName.lastIndexOf('.') + 1);
72 }
73
74 public static String getBaseName(String pathName){
75 if(pathName.lastIndexOf('.') == -1) return pathName;
76 return pathName.substring(0, pathName.lastIndexOf('.'));
77 }
78
79 public static String readFile(File file) throws IOException {
80 return readFile(file, null);
81 }
82
83 public static String readFile(File file, String encoding) throws IOException {
84 InputStream is = new FileInputStream(file);
85 long length = file.length();
86
87 if (length > Integer.MAX_VALUE) {
88 // File is too large
89 // TODO: we need some feedback for the case of the file being too large
90 }
91
92 // Create the byte array to hold the data
93 byte[] bytes = new byte[(int)length];
94
95 // Read in the bytes
96 int offset = 0;
97 int numRead = 0;
98 while (offset < bytes.length
99 && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
100 offset += numRead;
101 }
102
103 // Ensure all the bytes have been read in
104 if (offset < bytes.length) {
105 throw new IOException("Could not completely read file "+file.getName());
106 }
107
108 // Close the input stream and return bytes
109 is.close();
110 if(encoding == null){
111 return new String(bytes);
112 } else {
113 return new String(bytes, encoding);
114 }
115 }
116
117 public static void main(String[] args){
118 System.out.println(makeTargetPathname("C:\\Files\\papar.txt", "jpg", false));
119 System.out.println(makeTargetPathname("C:\\Files\\papar", "jpg", false));
120 System.out.println(makeTargetPathname("papar.txt", "jpg", false));
121 System.out.println(makeTargetPathname("/home/sideris/tsourekia/papar.txt", "jpg", false));
122 System.out.println(makeTargetPathname("D:\\diagram.max", "jpg", false));
123 System.out.println(makeAlternativePathname("C:\\Files\\papar.txt"));
124 System.out.println(makeAlternativePathname("C:\\Files\\papar"));
125 System.out.println(getExtension("pipi.jpeg"));
126 System.out.println(getExtension("pipi"));
127 }
128 }