001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalFilePermission;
018    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
019    
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.Reader;
025    
026    import java.util.List;
027    import java.util.Properties;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     */
033    public class FileUtil {
034    
035            public static void copyDirectory(File source, File destination)
036                    throws IOException {
037    
038                    PortalFilePermission.checkCopy(_getPath(source), _getPath(destination));
039    
040                    getFile().copyDirectory(source, destination);
041            }
042    
043            public static void copyDirectory(
044                            String sourceDirName, String destinationDirName)
045                    throws IOException {
046    
047                    PortalFilePermission.checkCopy(sourceDirName, destinationDirName);
048    
049                    getFile().copyDirectory(sourceDirName, destinationDirName);
050            }
051    
052            public static void copyFile(File source, File destination)
053                    throws IOException {
054    
055                    PortalFilePermission.checkCopy(_getPath(source), _getPath(destination));
056    
057                    getFile().copyFile(source, destination);
058            }
059    
060            public static void copyFile(File source, File destination, boolean lazy)
061                    throws IOException {
062    
063                    PortalFilePermission.checkCopy(_getPath(source), _getPath(destination));
064    
065                    getFile().copyFile(source, destination, lazy);
066            }
067    
068            public static void copyFile(String source, String destination)
069                    throws IOException {
070    
071                    PortalFilePermission.checkCopy(source, destination);
072    
073                    getFile().copyFile(source, destination);
074            }
075    
076            public static void copyFile(String source, String destination, boolean lazy)
077                    throws IOException {
078    
079                    PortalFilePermission.checkCopy(source, destination);
080    
081                    getFile().copyFile(source, destination, lazy);
082            }
083    
084            public static File createTempFile() {
085                    PortalFilePermission.checkWrite(
086                            SystemProperties.get(SystemProperties.TMP_DIR));
087    
088                    return getFile().createTempFile();
089            }
090    
091            public static File createTempFile(byte[] bytes) throws IOException {
092                    PortalFilePermission.checkWrite(
093                            SystemProperties.get(SystemProperties.TMP_DIR));
094    
095                    return getFile().createTempFile(bytes);
096            }
097    
098            public static File createTempFile(InputStream is) throws IOException {
099                    PortalFilePermission.checkWrite(
100                            SystemProperties.get(SystemProperties.TMP_DIR));
101    
102                    return getFile().createTempFile(is);
103            }
104    
105            public static File createTempFile(String extension) {
106                    PortalFilePermission.checkWrite(
107                            SystemProperties.get(SystemProperties.TMP_DIR));
108    
109                    return getFile().createTempFile(extension);
110            }
111    
112            public static String createTempFileName() {
113                    PortalFilePermission.checkWrite(
114                            SystemProperties.get(SystemProperties.TMP_DIR));
115    
116                    return getFile().createTempFileName();
117            }
118    
119            public static String createTempFileName(String extension) {
120                    PortalFilePermission.checkWrite(
121                            SystemProperties.get(SystemProperties.TMP_DIR));
122    
123                    return getFile().createTempFileName(extension);
124            }
125    
126            public static File createTempFolder() {
127                    PortalFilePermission.checkWrite(
128                            SystemProperties.get(SystemProperties.TMP_DIR));
129    
130                    return getFile().createTempFolder();
131            }
132    
133            public static String decodeSafeFileName(String fileName) {
134                    return getFile().decodeSafeFileName(fileName);
135            }
136    
137            public static boolean delete(File file) {
138                    PortalFilePermission.checkDelete(_getPath(file));
139    
140                    return getFile().delete(file);
141            }
142    
143            public static boolean delete(String file) {
144                    PortalFilePermission.checkDelete(file);
145    
146                    return getFile().delete(file);
147            }
148    
149            public static void deltree(File directory) {
150                    PortalFilePermission.checkDelete(_getPath(directory));
151    
152                    getFile().deltree(directory);
153            }
154    
155            public static void deltree(String directory) {
156                    PortalFilePermission.checkDelete(directory);
157    
158                    getFile().deltree(directory);
159            }
160    
161            public static String encodeSafeFileName(String fileName) {
162                    return getFile().encodeSafeFileName(fileName);
163            }
164    
165            public static boolean exists(File file) {
166                    PortalFilePermission.checkRead(_getPath(file));
167    
168                    return getFile().exists(file);
169            }
170    
171            public static boolean exists(String fileName) {
172                    PortalFilePermission.checkRead(fileName);
173    
174                    return getFile().exists(fileName);
175            }
176    
177            /**
178             * Extract text from an input stream and file name.
179             *
180             * @param  is input stream of file
181             * @param  fileName full name or extension of file (e.g., "Test.doc",
182             *         ".doc")
183             * @return Extracted text if it is a supported format or an empty string if
184             *         it is an unsupported format
185             */
186            public static String extractText(InputStream is, String fileName) {
187                    return getFile().extractText(is, fileName);
188            }
189    
190            public static String[] find(
191                    String directory, String includes, String excludes) {
192    
193                    PortalFilePermission.checkRead(directory);
194    
195                    return getFile().find(directory, includes, excludes);
196            }
197    
198            public static String getAbsolutePath(File file) {
199                    return getFile().getAbsolutePath(file);
200            }
201    
202            public static byte[] getBytes(File file) throws IOException {
203                    PortalFilePermission.checkRead(_getPath(file));
204    
205                    return getFile().getBytes(file);
206            }
207    
208            public static byte[] getBytes(InputStream is) throws IOException {
209                    return getFile().getBytes(is);
210            }
211    
212            public static byte[] getBytes(InputStream is, int bufferSize)
213                    throws IOException {
214    
215                    return getFile().getBytes(is);
216            }
217    
218            public static byte[] getBytes(
219                            InputStream is, int bufferSize, boolean cleanUpStream)
220                    throws IOException {
221    
222                    return getFile().getBytes(is, bufferSize, cleanUpStream);
223            }
224    
225            public static String getExtension(String fileName) {
226                    return getFile().getExtension(fileName);
227            }
228    
229            public static com.liferay.portal.kernel.util.File getFile() {
230                    PortalRuntimePermission.checkGetBeanProperty(FileUtil.class);
231    
232                    return _file;
233            }
234    
235            public static String getMD5Checksum(java.io.File file) throws IOException {
236                    return getFile().getMD5Checksum(file);
237            }
238    
239            public static String getPath(String fullFileName) {
240                    return getFile().getPath(fullFileName);
241            }
242    
243            public static String getShortFileName(String fullFileName) {
244                    return getFile().getShortFileName(fullFileName);
245            }
246    
247            public static boolean isAscii(File file) throws IOException {
248                    PortalFilePermission.checkRead(_getPath(file));
249    
250                    return getFile().isAscii(file);
251            }
252    
253            public static boolean isSameContent(File file, byte[] bytes, int length) {
254                    PortalFilePermission.checkRead(_getPath(file));
255    
256                    return getFile().isSameContent(file, bytes, length);
257            }
258    
259            public static boolean isSameContent(File file, String s) {
260                    PortalFilePermission.checkRead(_getPath(file));
261    
262                    return getFile().isSameContent(file, s);
263            }
264    
265            public static String[] listDirs(File file) {
266                    PortalFilePermission.checkRead(_getPath(file));
267    
268                    return getFile().listDirs(file);
269            }
270    
271            public static String[] listDirs(String fileName) {
272                    PortalFilePermission.checkRead(fileName);
273    
274                    return getFile().listDirs(fileName);
275            }
276    
277            public static String[] listFiles(File file) {
278                    PortalFilePermission.checkRead(_getPath(file));
279    
280                    return getFile().listFiles(file);
281            }
282    
283            public static String[] listFiles(String fileName) {
284                    PortalFilePermission.checkRead(fileName);
285    
286                    return getFile().listFiles(fileName);
287            }
288    
289            public static void mkdirs(String pathName) {
290                    PortalFilePermission.checkCopy(pathName, pathName);
291    
292                    getFile().mkdirs(pathName);
293            }
294    
295            public static boolean move(File source, File destination) {
296                    PortalFilePermission.checkMove(_getPath(source), _getPath(destination));
297    
298                    return getFile().move(source, destination);
299            }
300    
301            public static boolean move(
302                    String sourceFileName, String destinationFileName) {
303    
304                    PortalFilePermission.checkMove(sourceFileName, destinationFileName);
305    
306                    return getFile().move(sourceFileName, destinationFileName);
307            }
308    
309            public static String read(File file) throws IOException {
310                    PortalFilePermission.checkRead(_getPath(file));
311    
312                    return getFile().read(file);
313            }
314    
315            public static String read(File file, boolean raw) throws IOException {
316                    PortalFilePermission.checkRead(_getPath(file));
317    
318                    return getFile().read(file, raw);
319            }
320    
321            public static String read(String fileName) throws IOException {
322                    PortalFilePermission.checkRead(fileName);
323    
324                    return getFile().read(fileName);
325            }
326    
327            public static String replaceSeparator(String fileName) {
328                    return getFile().replaceSeparator(fileName);
329            }
330    
331            public static File[] sortFiles(File[] files) {
332                    return getFile().sortFiles(files);
333            }
334    
335            public static String stripExtension(String fileName) {
336                    return getFile().stripExtension(fileName);
337            }
338    
339            public static List<String> toList(Reader reader) {
340                    return getFile().toList(reader);
341            }
342    
343            public static List<String> toList(String fileName) {
344                    return getFile().toList(fileName);
345            }
346    
347            public static Properties toProperties(FileInputStream fis) {
348                    return getFile().toProperties(fis);
349            }
350    
351            public static Properties toProperties(String fileName) {
352                    PortalFilePermission.checkRead(fileName);
353    
354                    return getFile().toProperties(fileName);
355            }
356    
357            public static void touch(File file) throws IOException {
358                    PortalFilePermission.checkWrite(_getPath(file));
359    
360                    getFile().touch(file);
361            }
362    
363            public static void touch(String fileName) throws IOException {
364                    PortalFilePermission.checkWrite(fileName);
365    
366                    getFile().touch(fileName);
367            }
368    
369            public static void unzip(File source, File destination) {
370                    PortalFilePermission.checkCopy(_getPath(source), _getPath(destination));
371    
372                    getFile().unzip(source, destination);
373            }
374    
375            public static void write(File file, byte[] bytes) throws IOException {
376                    write(file, bytes, false);
377            }
378    
379            public static void write(File file, byte[] bytes, boolean append)
380                    throws IOException {
381    
382                    PortalFilePermission.checkWrite(_getPath(file));
383    
384                    getFile().write(file, bytes, append);
385            }
386    
387            public static void write(File file, byte[] bytes, int offset, int length)
388                    throws IOException {
389    
390                    write(file, bytes, offset, length, false);
391            }
392    
393            public static void write(
394                            File file, byte[] bytes, int offset, int length, boolean append)
395                    throws IOException {
396    
397                    PortalFilePermission.checkWrite(_getPath(file));
398    
399                    getFile().write(file, bytes, offset, length, append);
400            }
401    
402            public static void write(File file, InputStream is) throws IOException {
403                    PortalFilePermission.checkWrite(_getPath(file));
404    
405                    getFile().write(file, is);
406            }
407    
408            public static void write(File file, String s) throws IOException {
409                    PortalFilePermission.checkWrite(_getPath(file));
410    
411                    getFile().write(file, s);
412            }
413    
414            public static void write(File file, String s, boolean lazy)
415                    throws IOException {
416    
417                    PortalFilePermission.checkWrite(_getPath(file));
418    
419                    getFile().write(file, s, lazy);
420            }
421    
422            public static void write(File file, String s, boolean lazy, boolean append)
423                    throws IOException {
424    
425                    PortalFilePermission.checkWrite(_getPath(file));
426    
427                    getFile().write(file, s, lazy, append);
428            }
429    
430            public static void write(String fileName, byte[] bytes) throws IOException {
431                    PortalFilePermission.checkWrite(fileName);
432    
433                    getFile().write(fileName, bytes);
434            }
435    
436            public static void write(String fileName, InputStream is)
437                    throws IOException {
438    
439                    PortalFilePermission.checkWrite(fileName);
440    
441                    getFile().write(fileName, is);
442            }
443    
444            public static void write(String fileName, String s) throws IOException {
445                    PortalFilePermission.checkWrite(fileName);
446    
447                    getFile().write(fileName, s);
448            }
449    
450            public static void write(String fileName, String s, boolean lazy)
451                    throws IOException {
452    
453                    PortalFilePermission.checkWrite(fileName);
454    
455                    getFile().write(fileName, s, lazy);
456            }
457    
458            public static void write(
459                            String fileName, String s, boolean lazy, boolean append)
460                    throws IOException {
461    
462                    PortalFilePermission.checkWrite(fileName);
463    
464                    getFile().write(fileName, s, lazy, append);
465            }
466    
467            public static void write(String pathName, String fileName, String s)
468                    throws IOException {
469    
470                    PortalFilePermission.checkWrite(pathName);
471    
472                    getFile().write(pathName, fileName, s);
473            }
474    
475            public static void write(
476                            String pathName, String fileName, String s, boolean lazy)
477                    throws IOException {
478    
479                    PortalFilePermission.checkWrite(pathName);
480    
481                    getFile().write(pathName, fileName, s, lazy);
482            }
483    
484            public static void write(
485                            String pathName, String fileName, String s, boolean lazy,
486                            boolean append)
487                    throws IOException {
488    
489                    PortalFilePermission.checkWrite(pathName);
490    
491                    getFile().write(pathName, fileName, s, lazy, append);
492            }
493    
494            public void setFile(com.liferay.portal.kernel.util.File file) {
495                    PortalRuntimePermission.checkSetBeanProperty(getClass());
496    
497                    _file = file;
498            }
499    
500            private static String _getPath(File file) {
501                    if (file == null) {
502                            return null;
503                    }
504    
505                    return file.getPath();
506            }
507    
508            private static com.liferay.portal.kernel.util.File _file;
509    
510    }