Skip to content

Instantly share code, notes, and snippets.

@timyates
Last active August 29, 2015 14:20
Show Gist options
  • Save timyates/b3cae0cddda8b6c832b6 to your computer and use it in GitHub Desktop.
Save timyates/b3cae0cddda8b6c832b6 to your computer and use it in GitHub Desktop.
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
/**
* $Id: $
*/
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
public class JavaSecuredFile extends File {
public JavaSecuredFile(String s) {
super(s);
}
public JavaSecuredFile(String s, String s1) {
super(s, s1);
}
public JavaSecuredFile(File file, String s) {
super(file, s);
}
public JavaSecuredFile(URI uri) {
super(uri);
}
private JavaSecuredFile wrap(String file) {
if(file == null) {
return null;
}
else {
return new JavaSecuredFile(file, "");
}
}
@Override
public File getParentFile() {
return wrap(super.getParent());
}
@Override
public File getAbsoluteFile() {
return wrap(super.getAbsolutePath());
}
@Override
public File getCanonicalFile() throws IOException {
return wrap(super.getCanonicalPath());
}
@Override
public boolean canRead() {
try {
return super.canRead();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean canWrite() {
try {
return super.canRead();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean exists() {
try {
return super.exists();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean isDirectory() {
try {
return super.isDirectory();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean isFile() {
try {
return super.isFile();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean isHidden() {
try {
return super.isHidden();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean delete() {
try {
return super.delete();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean mkdir() {
try {
return super.mkdir();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean mkdirs() {
try {
return super.mkdirs();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean renameTo(File file) {
try {
return super.renameTo(file);
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean setLastModified(long l) {
try {
return super.setLastModified(l);
}
catch(SecurityException ex) {
return false;
}
}
@Override
public boolean setReadOnly() {
try {
return super.setReadOnly();
}
catch(SecurityException ex) {
return false;
}
}
@Override
public String getCanonicalPath() throws IOException {
try {
return super.getCanonicalPath();
}
catch(SecurityException ex) {
throw new IOException(ex);
}
}
@Override
public boolean createNewFile() throws IOException {
try {
return super.createNewFile();
}
catch(SecurityException ex) {
throw new IOException(ex);
}
}
@Override
public String[] list() {
try {
return super.list();
}
catch(SecurityException ex) {
return null;
}
}
@Override
public String[] list(FilenameFilter filenameFilter) {
try {
return super.list(filenameFilter);
}
catch(SecurityException ex) {
return null;
}
}
@Override
public File[] listFiles() {
try {
return super.listFiles();
}
catch(SecurityException ex) {
return null;
}
}
@Override
public File[] listFiles(FileFilter fileFilter) {
try {
return super.listFiles(fileFilter);
}
catch(SecurityException ex) {
return null;
}
}
@Override
public File[] listFiles(FilenameFilter filenameFilter) {
try {
return super.listFiles(filenameFilter);
}
catch(SecurityException ex) {
return null;
}
}
@Override
public long lastModified() {
try {
return super.lastModified();
}
catch(SecurityException ex) {
return 0L;
}
}
@Override
public long length() {
try {
return super.length();
}
catch(SecurityException ex) {
return 0L;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment