Package se.datadosen.util
Class IO
java.lang.Object
se.datadosen.util.IO
Utility class to simplify common file related operations This class is LGPL
licensed. Just keep this notice intact
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
static final String
static final String
static final ExecutorService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
static String
static String
baseName
(AlbumObject ao) static void
static String
combinePaths
(String part1, String part2) Combine two path parts so that there is one and only one path separator between nomatter how each part looks.static String
combinePaths
(String part1, String part2, char separator) Combine two path parts so that there is one and only one path separator between no matter how each part looks.static void
copyDirectoryContent
(File srcDir, File dest, boolean forceCopy) static void
copyDirectoryContent
(File srcDir, File dest, boolean forceCopy, FileFilter filter) static void
copyDirectoryContent
(File srcDir, File dest, boolean forceCopy, FileFilter filter, boolean createHardLink) static void
copyDirectoryContent
(String srcPath, File dest, boolean forceCopy) Copy a directory (including its subdirectories) to destination directory.static void
Copy file or directory to destination.static void
Copy file or directory to destination.static boolean
Copy file or directory to destination.static void
Copy file or directory to destination.static void
Copy file or directory to destination.static void
copyFilesUI
(JFrame parent, File[] files, File dest) static void
createDirectories
(Path path, FileAttribute<?>... attrs) Like Files.createDirectories, but won't throw FileAlreadyExistsException if path already exists and is a symlink to a directorystatic long
deepLastModified
(File dir) Figure out the latest "last modified" file date of any file or directory under the directory passed as argument including files in subdirectoriesstatic long
deepLastModifiedFile
(File dir) Figure out the latest "last modified" file date of any file (not directory) under the directory passed as argument including files in subdirectoriesstatic void
The very powerful (and dangerous) delete directory Performs true deletes since jAlbum 8.7.2.static File
downloadFile
(URL url, File dest) Download file to destination folder or filestatic void
downloadFile
(URL url, OutputStream out) Download file to output stream Uses a timeout of 10 secondsstatic void
downloadZipFile
(URL url, File destFolder) static void
Delete directory contents recursively, but leave directory Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)static File
ensureUnique
(File dir, String suggestedName) static String
extensionOf
(File file) static String
extensionOf
(String name) static boolean
static boolean
isSubdirectoryOf
(File subdir, File dir) Test if subdir is a subdirectory of dir (or one of its subdirectories recursively)static boolean
isThumbFile
(File file) static boolean
static String
lastPathComponentOf
(String path) static String
parentPath
(String path) static String
Since 7.2static byte[]
static Map
readMap
(byte[] bytes) static Map
readMapFile
(File file) Read an equal sign separated list of name=value pairs into a Map object.static Properties
readPropertyFile
(File file) Read an equal sign separated list of name=value pairs into a Property object.static String
readTextFile
(File file) static String
readTextFile
(File file, String encoding) Read a text file into a String.static String
readTextFile
(String fileName) Since v15.5 reimplemented to throw NoSuchFileException and PermissionDeniedExceptionstatic String
readTextUrl
(URL textUrl) static String
readTextUrl
(URL textUrl, String userAgent) static String
readTextUrl
(URL textUrl, SSLSocketFactory socketFactory) static String
readTextUrl
(URL textUrl, SSLSocketFactory socketFactory, String userAgent) static boolean
Will attempt to move the directory to the recycle bin If recycling fails, will attempt to delete permanently On Mac.static boolean
Will attempt to move the directory to the recycle bin On Mac.static String
relativePath
(File file, File rel) static String
relativePath
(String fileString, String relString) static String
relativePath
(String fileString, String relString, char separator) static File
resolvePath
(File f, String path) static void
setPreserveLastModified
(boolean preserveLastModified) static int
Return number of bytes a serialized version of the passed object would occupystatic File
Get the corresponding thumbnail file for given filestatic int
static int
unzip
(ZipInputStream zis, File destDir) static int
unzip
(ZipInputStream zis, File destDir, boolean downloading) static String
URLencoding based on rfc3986 but with additions to reserved words to comply with Mac OSX restrictions; Reference: http://en.wikipedia.org/wiki/Percent-encoding Prior to jAlbum 8.1.5, this encoder attempted ISO8859-1 encoding of characters > 127 Only if a character wasn't encodable using ISO8859-1 it reverted to UTF-8 encoding.static File
static String
static void
writeBytes
(byte[] buf, File f) static boolean
writeChangedTextFile
(String content, File file, String encoding) Deprecated.Use TargetPage instead of improved parallelismstatic void
writeMapFile
(Map map, File f) Write a Map to file.static void
writeTextFile
(String content, File file) static void
writeTextFile
(String content, File file, String encoding) static File
Zip a file or folder to destination file.static void
Zip a file or folder to destination filestatic File
zip
(File f, FileFilter filter) Zip a file or folder to destination file.static void
zip
(File f, File zipFile, FileFilter filter) Zip a file or folder to destination file
-
Field Details
-
CHROME_USER_AGENT
- Since:
- 28.2
- See Also:
-
workerPool
-
rfc3986Reserved
- See Also:
-
reserved
- See Also:
-
-
Constructor Details
-
IO
public IO()
-
-
Method Details
-
ensureUnique
-
readTextFile
public static String readTextFile(File file, String encoding) throws IOException, NoSuchFileException, PermissionDeniedException Read a text file into a String. Autodetects UTF-8 and UTF-16 encodings- Parameters:
file
-encoding
-- Returns:
- Throws:
IOException
NoSuchFileException
PermissionDeniedException
-
isPreserveLastModified
public static boolean isPreserveLastModified() -
setPreserveLastModified
public static void setPreserveLastModified(boolean preserveLastModified) -
readTextFile
public static String readTextFile(File file) throws IOException, NoSuchFileException, PermissionDeniedException -
readTextFile
public static String readTextFile(String fileName) throws IOException, NoSuchFileException, PermissionDeniedException Since v15.5 reimplemented to throw NoSuchFileException and PermissionDeniedException- Parameters:
fileName
-- Returns:
- Throws:
IOException
NoSuchFileException
PermissionDeniedException
-
readTextUrl
- Throws:
IOException
-
readTextUrl
- Parameters:
textUrl
-userAgent
-- Returns:
- Throws:
IOException
- Since:
- 28.2
-
readTextUrl
- Parameters:
textUrl
-socketFactory
-- Returns:
- Throws:
IOException
- Since:
- 20
-
readTextUrl
public static String readTextUrl(URL textUrl, SSLSocketFactory socketFactory, String userAgent) throws IOException - Parameters:
textUrl
-socketFactory
-userAgent
-- Returns:
- Throws:
IOException
- Since:
- 28.2
-
readBytes
public static byte[] readBytes(File f) throws IOException, NoSuchFileException, PermissionDeniedException -
writeBytes
- Throws:
IOException
-
writeTextFile
- Throws:
IOException
-
writeTextFile
- Throws:
IOException
-
writeChangedTextFile
@Deprecated public static boolean writeChangedTextFile(String content, File file, String encoding) throws IOException Deprecated.Use TargetPage instead of improved parallelismWrite text to text file if this means a change to the file content- Parameters:
content
- Text to writefile
- File to be written. Will make a comparison before writing if file already existsencoding
- Text encoding to use- Returns:
- boolean true if a change was actually made
- Throws:
IOException
- Since:
- v4.5
-
readPropertyFile
Read an equal sign separated list of name=value pairs into a Property object. Does not handle names with spaces and is limited to ISO-8859-1. See readMapFile()- Parameters:
file
-- Returns:
- Throws:
IOException
-
readMapFile
Read an equal sign separated list of name=value pairs into a Map object. Handles names containing spaces. Respects current system encoding and auto-detects UTF-8 and UTF-16 encoding. Lines beginning with # are considered comment lines Values ending in backslash is considered to be a multi-line value Backslashes in values are expressed as double backslashes- Parameters:
file
-- Returns:
- Throws:
IOException
-
readMap
- Throws:
IOException
- Since:
- 8.0
- See Also:
-
writeMapFile
Write a Map to file. Tries to write using system encoding. If that doesn't work, uses UTF-8. Multi-line values result in lines ending in backslashes Backslashes in values generate double backslashes- Parameters:
map
-f
-- Throws:
IOException
-
copyFilesUI
public static void copyFilesUI(JFrame parent, File[] files, File dest) throws OperationAbortedException - Throws:
OperationAbortedException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
src
- to copydest
- Destination. May point either to a file or a directory- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
name
- Name of file to copydest
- Destination. May point either to a file or a directory- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
name
- Name of file to copydest
- Destination. May point either to a file or a directoryforceCopy
- Always copy, even if destination file already exist- Throws:
IOException
-
copyFile
Copy file or directory to destination. Preserves file modification date- Parameters:
src
- File to copydest
- Destination. May point either to a file or a directoryforceCopy
- Always copy, even if destination file already exist- Throws:
IOException
-
copyFile
public static boolean copyFile(File src, File dest, boolean forceCopy, boolean createHardLink) throws IOException Copy file or directory to destination. Preserves file modification date- Parameters:
src
- File to copydest
- Destination. May point either to a file or a directoryforceCopy
- Always copy, even if destination file already existcreateHardLink
- Attempt to make a hard link instead of copy- Returns:
- true if directed to make hard link, unless linking fails
- Throws:
IOException
-
downloadFile
Download file to destination folder or file- Parameters:
url
- Where to get filedest
- File or folder to write to- Returns:
- Destination file
- Throws:
PermissionDeniedException
- if file can't be createdIOException
- if file can't be read- Since:
- 11
-
downloadFile
Download file to output stream Uses a timeout of 10 seconds- Parameters:
url
-out
-- Throws:
IOException
- Since:
- 11
-
downloadZipFile
- Throws:
IOException
-
readAll
Since 7.2- Parameters:
in
- Reader- Returns:
- String
- Throws:
IOException
-
isSubdirectoryOf
Test if subdir is a subdirectory of dir (or one of its subdirectories recursively) -
copyDirectoryContent
public static void copyDirectoryContent(String srcPath, File dest, boolean forceCopy) throws IOException Copy a directory (including its subdirectories) to destination directory.- Parameters:
srcPath
- Path to source directorydest
- Destination directory. Create one if not existing alreadyforceCopy
- Always copy, even if destination files already exists- Throws:
IOException
-
copyDirectoryContent
public static void copyDirectoryContent(File srcDir, File dest, boolean forceCopy) throws IOException - Throws:
IOException
-
copyDirectoryContent
public static void copyDirectoryContent(File srcDir, File dest, boolean forceCopy, FileFilter filter) throws IOException - Throws:
IOException
-
copyDirectoryContent
public static void copyDirectoryContent(File srcDir, File dest, boolean forceCopy, FileFilter filter, boolean createHardLink) throws IOException - Throws:
IOException
-
createDirectories
Like Files.createDirectories, but won't throw FileAlreadyExistsException if path already exists and is a symlink to a directory- Parameters:
path
-attrs
-- Throws:
IOException
- Since:
- 13.7
-
parentPath
- Parameters:
path
-- Returns:
- parent path (stripping stuff after last /)
- Since:
- 24
-
baseName
- Parameters:
ao
-- Returns:
- Base name if AlbumObject is not a folder. Folders returns their full name
- Since:
- 28
-
baseName
- Returns:
- filename without extension
-
thumbFile
Get the corresponding thumbnail file for given file- Since:
- 8.0.9.1
-
isThumbFile
-
baseName
- Returns:
- filename without extension
-
extensionOf
- Returns:
- extension of file name
-
lastPathComponentOf
- Since:
- 13.5
-
extensionOf
- Returns:
- extension of file name
-
relativePath
- Parameters:
file
-rel
-- Returns:
- a path to file that is relative to rel
-
relativePath
- Since:
- 7.0.4
-
relativePath
- Since:
- 7.1
-
urlEncode
URLencoding based on rfc3986 but with additions to reserved words to comply with Mac OSX restrictions; Reference: http://en.wikipedia.org/wiki/Percent-encoding Prior to jAlbum 8.1.5, this encoder attempted ISO8859-1 encoding of characters > 127 Only if a character wasn't encodable using ISO8859-1 it reverted to UTF-8 encoding. Now UTF-8 is used for all characters > 127 -
webSafe
- Parameters:
fileName
-- Returns:
- Web safe version of file name that doesn't contain any illegal characters (replaced with -)
- Since:
- 11.3
-
isWebSafe
-
webSafe
-
combinePaths
Combine two path parts so that there is one and only one path separator between nomatter how each part looks.- Parameters:
part1
-part2
-- Returns:
- String combined path (using / as separator
-
combinePaths
Combine two path parts so that there is one and only one path separator between no matter how each part looks.- Parameters:
part1
-part2
-separator
-- Returns:
- String combined path
-
sizeof
Return number of bytes a serialized version of the passed object would occupy- Parameters:
o
- Object, must implement serializable- Returns:
- Throws:
RuntimeException
-
deepLastModified
Figure out the latest "last modified" file date of any file or directory under the directory passed as argument including files in subdirectories- Parameters:
dir
-- Returns:
- Throws:
IOException
- Since:
- 5.2
-
deepLastModifiedFile
Figure out the latest "last modified" file date of any file (not directory) under the directory passed as argument including files in subdirectories- Throws:
IOException
- Since:
- 8.4.1
-
close
-
deleteDir
The very powerful (and dangerous) delete directory Performs true deletes since jAlbum 8.7.2. Previously attempted to recycle first Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)- Parameters:
dir
- Directory to delete- Since:
- 8.5
-
emptyDir
Delete directory contents recursively, but leave directory Does not follow symbolic links when running on Java 7+ (Since jAlbum 12.1)- Parameters:
dir
-- Since:
- v12
-
recycle
Will attempt to move the directory to the recycle bin If recycling fails, will attempt to delete permanently On Mac. Java version 1.6.0_17 and similar on Java 1.5 is required- Parameters:
dir
- Directory to recycledir
-- Returns:
- true if directory was recycled instead of deleted
- Since:
- 8.7.2
-
recycle
Will attempt to move the directory to the recycle bin On Mac. Java version 1.6.0_17 and similar on Java 1.5 is required- Parameters:
dir
- Directory to recyclerecycleOnly
- If false, will attempt to delete permanently if recycling fails- Returns:
- true if directory was recycled instead of deleted
- Since:
- 8.7.2
-
resolvePath
-
zip
Zip a file or folder to destination file. Put resulting zip file next to file- Parameters:
f
- File or directory to zip- Returns:
- zipped file
- Throws:
ZipException
IOException
- Since:
- 16
-
zip
Zip a file or folder to destination file- Parameters:
f
- File or directory to zipzipFile
- File Target for zipping- Throws:
ZipException
IOException
- Since:
- 16
-
zip
public static File zip(File f, FileFilter filter) throws ZipException, IOException, OperationAbortedException Zip a file or folder to destination file. Put resulting zip file next to file- Parameters:
f
- File or directory to zipfilter
- Allows monitoring each each zipped file or rejection- Returns:
- zipped file
- Throws:
ZipException
IOException
OperationAbortedException
- Since:
- 16
-
zip
public static void zip(File f, File zipFile, FileFilter filter) throws ZipException, IOException, OperationAbortedException Zip a file or folder to destination file- Parameters:
f
- File or directory to zipzipFile
- File Target for zippingfilter
- Allows monitoring each each zipped file or rejection. Throw OperationAbortedException to abort- Throws:
ZipException
IOException
OperationAbortedException
- Since:
- 16
-
unzip
- Throws:
ZipException
IOException
-
unzip
- Throws:
ZipException
IOException
-
unzip
public static int unzip(ZipInputStream zis, File destDir, boolean downloading) throws ZipException, IOException - Throws:
ZipException
IOException
-