Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /**
2 * ditaa - Diagrams Through Ascii Art
3 *
•4 * Copyright (C) 2004-2011 Efstathios Sideris
5 *
•6 * ditaa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
•11 * ditaa 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 Lesser General Public License for more details.
15 *
•16 * You should have received a copy of the GNU Lesser General Public
17 * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package org.stathissideris.ascii2image.core;
21
22 import java.io.File;
23 import java.io.FileInputStream;
•24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
•27 import java.io.InputStreamReader;
28 import java.io.LineNumberReader;
29
30 /**
31 *
32 * @author Efstathios Sideris
33 */
34 public class FileUtils {
35
36 //private static final
37
38 public static String makeTargetPathname(String sourcePathname, String extension, boolean overwrite){
39 return makeTargetPathname(sourcePathname, extension, "", overwrite);
40 }
41
42 public static String makeTargetPathname(String sourcePathname, String extension, String postfix, boolean overwrite){
43 File sourceFile =
44 new File(sourcePathname);
45
46 String path = "";
47 if(sourceFile.getParentFile() != null){
48 path = sourceFile.getParentFile().getAbsolutePath();
49 if(!path.endsWith(File.separator)) path += File.separator;
50 }
51 String baseName = getBaseName(sourceFile.getName());
52
53 String targetName = path + baseName + postfix + "." + extension;
54 if(new File(targetName).exists() && !overwrite)
55 targetName = makeAlternativePathname(targetName);
56 return targetName;
57 }
58
59 public static String makeAlternativePathname(String pathName){
60 int limit = 100;
61
62 for(int i = 2; i <= limit; i++){
63 String alternative = getBaseName(pathName)+"_"+i;
64 String extension = getExtension(pathName);
65 if(extension != null) alternative += "."+extension;
66 if(!(new File(alternative).exists())) return alternative;
67 }
68 return null;
69 }
70
71 public static String getExtension(String pathName){
72 if(pathName.lastIndexOf('.') == -1) return null;
73 return pathName.substring(pathName.lastIndexOf('.') + 1);
74 }
75
76 public static String getBaseName(String pathName){
77 if(pathName.lastIndexOf('.') == -1) return pathName;
78 return pathName.substring(0, pathName.lastIndexOf('.'));
79 }
80
81 public static String readFile(File file) throws IOException {
82 return readFile(file, null);
83 }
84
85 public static String readFile(File file, String encoding) throws IOException {
86 long length = file.length();
87
88 if (length > Integer.MAX_VALUE) {
89 // File is too large
90 // TODO: we need some feedback for the case of the file being too large
91 }
•92
93 return readFile(new FileInputStream(file), file.getName(), encoding, length);
94 }
95
96 public static String readFile(InputStream is, String name, String encoding) throws IOException {
97 return readFile(is, name, encoding, -1);
98 }
99
100 public static String readFile(InputStream is, String name, String encoding, long length) throws IOException {
101
102 if (length < 0) {
103 LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
104 StringBuilder builder = new StringBuilder();
105 while (true) {
106 String line = reader.readLine();
107 if (line == null) break;
108 else builder.append(line).append("\n");
109 }
110 return builder.toString();
111 }
112
113 else {
114 // Create the byte array to hold the data
115 byte[] bytes = new byte[(int)length];
116
•117 // Read in the bytes
118 int offset = 0;
119 int numRead = 0;
120 while (offset < bytes.length
121 && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
122 offset += numRead;
123 }
124
•125 // Ensure all the bytes have been read in
126 if (offset < bytes.length) {
127 throw new IOException("Could not completely read file "+name);
128 }
129
•130 // Close the input stream and return bytes
131 is.close();
132 if(encoding == null){
133 return new String(bytes);
•134 } else {
135 return new String(bytes, encoding);
•136 }
137 }
138 }
139
140 public static void main(String[] args){
141 System.out.println(makeTargetPathname("C:\\Files\\papar.txt", "jpg", false));
142 System.out.println(makeTargetPathname("C:\\Files\\papar", "jpg", false));
143 System.out.println(makeTargetPathname("papar.txt", "jpg", false));
144 System.out.println(makeTargetPathname("/home/sideris/tsourekia/papar.txt", "jpg", false));
145 System.out.println(makeTargetPathname("D:\\diagram.max", "jpg", false));
146 System.out.println(makeAlternativePathname("C:\\Files\\papar.txt"));
147 System.out.println(makeAlternativePathname("C:\\Files\\papar"));
148 System.out.println(getExtension("pipi.jpeg"));
149 System.out.println(getExtension("pipi"));
150 }
151 }