Package Torello.Java
Class SameSourceAndTargetException
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- java.lang.RuntimeException
-
- Torello.Java.SameSourceAndTargetException
-
- All Implemented Interfaces:
java.io.Serializable
public class SameSourceAndTargetException extends java.lang.RuntimeException
Thrown when a move or copy operation has specified identicalsource
andtarget
places on disk.
ThisException
is used to identify scenarios where a user has requested a move operation be performed on either a file, a directory, or a directory-tree where the source and target files (or directories) are identical.
This can have disastrous implications whereby after an initial Copy Phase, the Source-Code Logic then attempts to perform a Delete Phase whereby the original files are then removed.
The potential disaster to avoid is where the copy writes out the data to the same location (because of a user error where the provided source and target locations were identical). If the exception is not thrown immediately, when the delete begins, both versions are erased from disk, and the original data is simply wiped / erased completely.
This exception was initially written for:FileRW.copyFile
- See Also:
- Serialized Form
Hi-Lited Source-Code:- View Here: Torello/Java/SameSourceAndTargetException.java
- Open New Browser-Tab: Torello/Java/SameSourceAndTargetException.java
File Size: 4,447 Bytes Line Count: 97 '\n' Characters Found
-
-
Field Summary
Serializable ID Modifier and Type Field static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description SameSourceAndTargetException()
Constructs aSameSourceAndTargetException
with no detail message.SameSourceAndTargetException(String message)
Constructs aSameSourceAndTargetException
with the specified detail message.SameSourceAndTargetException(String message, Throwable cause)
Constructs a new exception with the specified detail message and cause.SameSourceAndTargetException(Throwable cause)
Constructs a new exception with the specified cause and a detail message of(cause==null ? null : cause.toString())
(which typically contains the class and detail message of cause).
-
Method Summary
'static'
Exception Check MethodsModifier and Type Method static void
check(FileNode directory, String targetDirectory)
-
-
-
Field Detail
-
serialVersionUID
public static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable
. Using theSerializable
Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.
Note that Java'sjava.lang.Exception
andjava.lang.Error
classes implement theSerializable interface
, and a warning-free build expects this field be defined here.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final long serialVersionUID = 1;
-
-
Constructor Detail
-
SameSourceAndTargetException
public SameSourceAndTargetException()
Constructs aSameSourceAndTargetException
with no detail message.
-
SameSourceAndTargetException
public SameSourceAndTargetException(java.lang.String message)
Constructs aSameSourceAndTargetException
with the specified detail message.- Parameters:
message
- the detail message.
-
SameSourceAndTargetException
public SameSourceAndTargetException(java.lang.String message, java.lang.Throwable cause)
Constructs a new exception with the specified detail message and cause.
NOTE:
The detail message associated with cause is not automatically incorporated into this exception's detail message.- Parameters:
message
- The detail message (which is saved for later retrieval by theThrowable.getMessage()
method).cause
- the cause (which is saved for later retrieval by theThrowable.getCause()
method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
-
SameSourceAndTargetException
public SameSourceAndTargetException(java.lang.Throwable cause)
Constructs a new exception with the specified cause and a detail message of(cause==null ? null : cause.toString())
(which typically contains the class and detail message of cause). This constructor is useful for exceptions that are little more than wrappers for otherThrowable's
.- Parameters:
cause
- The cause (which is saved for later retrieval by theThrowable.getCause()
method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
-
-
Method Detail
-
check
public static void check(FileNode directory, java.lang.String targetDirectory) throws java.io.IOException, SameSourceAndTargetException, java.nio.file.InvalidPathException, java.nio.file.NoSuchFileException
This method can be used to check whether the source and target directories in a program are actually pointing to the same location on the file-system. If they are, or if there are any problems in attempting to ascertain this information, then exceptions will be thrown.- Parameters:
directory
- This may be any instance ofFileNode
that represents a directory.targetDirectory
- This is ajava.lang.String
that represents the target directory.- Throws:
SameSourceAndTargetException
- If after obtaining the 'Real Path' of both thesource
and thetarget
directories, both real-path's indicate the same location on the file-system, then this exception will throw.java.nio.file.InvalidPathException
- If the Java Virtual Machine is unable to instantiate an instance ofjava.nio.files.Path
for either the'directory'
parameter or the'targetDirectoryParameter'
, then this exception will be thrown.java.nio.file.NoSuchFileException
- If, after instantiating an instance ofPath
for either thesource
or thetarget
directories, the Java Virtual Machine is unable to build an instance ofPath
using the methodPath.toRealPath()
, then this exception will throw.java.io.IOException
- If the method'Path.toRealPath()'
fails with any I/O problems then ths exception will throw- Code:
- Exact Method Body:
String sourceDir = Paths.get(directory.getFullPathName()).toRealPath().toString(); if (sourceDir.equals(Paths.get(targetDirectory).toRealPath().toString())) throw new SameSourceAndTargetException( "The source directory: \n\t" + directory.getFullPathName() + '\n' + "And the target directory: \n\t" + targetDirectory + '\n' + "Have the same real-path: \n\t" + sourceDir );
-
-