001/* 002 * Copyright 2011-2012 Stephen Connolly. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.jszip.pseudo.io; 018 019import org.sonatype.aether.util.StringUtils; 020 021import java.io.File; 022import java.io.FileFilter; 023import java.io.FilenameFilter; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027 028public abstract class PseudoFile { 029 private final PseudoFile parent; 030 031 public PseudoFile(PseudoFile parent) { 032 this.parent = parent; 033 } 034 035 public final int compareTo(PseudoFile pathname) { 036 return getAbsolutePath().compareTo(pathname.getAbsolutePath()); 037 } 038 039 public final String getAbsolutePath() { 040 return getAbsolutePath(PseudoFileSystem.current()); 041 } 042 043 public final String getAbsolutePath(PseudoFileSystem fs) { 044 StringBuilder result = new StringBuilder(); 045 final String pathSeparator = fs.getPathSeparator(); 046 if (parent != null) { 047 String parentPath = parent.getAbsolutePath(fs); 048 if (!StringUtils.isEmpty(parentPath)) { 049 result.append(parentPath); 050 if (!parentPath.equals(pathSeparator)) { 051 result.append(pathSeparator); 052 } 053 } 054 } else { 055 result.append(pathSeparator); 056 } 057 result.append(getName()); 058 return result.toString(); 059 } 060 061 public final PseudoFile getAbsoluteFile() { 062 return this; 063 } 064 065 public final String getCanonicalPath() throws IOException { 066 return getAbsolutePath(); 067 } 068 069 public final PseudoFile getCanonicalFile() throws IOException { 070 return this; 071 } 072 073 public final String getPath() { 074 return getAbsolutePath(); 075 } 076 077 public final String getParent() { 078 return parent == null ? null : parent.getAbsolutePath(); 079 } 080 081 public final PseudoFile getParentFile() { 082 return parent; 083 } 084 085 public final boolean isAbsolute() { 086 return true; 087 } 088 089 public final String[] list() { 090 PseudoFile[] children = PseudoFileSystem.current().listChildren(this, PseudoFileFilter.FILTER_NONE); 091 String[] result = new String[children.length]; 092 for (int i = 0; i < children.length; i++) { 093 result[i] = children[i].getName(); 094 } 095 return result; 096 } 097 098 public final String[] list(final FilenameFilter filter) { 099 PseudoFile[] children = PseudoFileSystem.current().listChildren(this, new PseudoFileFilter() { 100 final File fakeDir = new File(getAbsolutePath()); 101 102 public boolean accept(String name) { 103 return filter.accept(fakeDir, name); 104 } 105 }); 106 String[] result = new String[children.length]; 107 for (int i = 0; i < children.length; i++) { 108 result[i] = children[i].getName(); 109 } 110 return result; 111 } 112 113 public final PseudoFile[] listFiles() { 114 return PseudoFileSystem.current().listChildren(this, PseudoFileFilter.FILTER_NONE); 115 } 116 117 public final PseudoFile[] listFiles(final FilenameFilter filter) { 118 return PseudoFileSystem.current().listChildren(this, new PseudoFileFilter() { 119 final File fakeDir = new File(getAbsolutePath()); 120 121 public boolean accept(String name) { 122 return filter.accept(fakeDir, name); 123 } 124 }); 125 } 126 127 public final PseudoFile[] listFiles(final FileFilter filter) { 128 final String fakeDir = getAbsolutePath(); 129 return PseudoFileSystem.current().listChildren(this, new PseudoFileFilter() { 130 public boolean accept(String name) { 131 return filter.accept(new File(fakeDir + "/" + name)); 132 } 133 }); 134 } 135 136 @Override 137 public final boolean equals(Object o) { 138 if (this == o) { 139 return true; 140 } 141 if (!(o instanceof PseudoFile)) { 142 return false; 143 } 144 145 PseudoFile that = (PseudoFile) o; 146 147 final PseudoFile thisParentFile = getParentFile(); 148 final PseudoFile thatParentFile = that.getParentFile(); 149 if (thisParentFile != null ? !thisParentFile.equals(thatParentFile) : thatParentFile != null) { 150 return false; 151 } 152 153 final String thisName = getName(); 154 final String thatName = that.getName(); 155 if (thisName != null ? !thisName.equals(thatName) : thatName != null) { 156 return false; 157 } 158 159 return true; 160 } 161 162 @Override 163 public final int hashCode() { 164 int result = parent != null ? parent.hashCode() : 0; 165 final String name = getName(); 166 result = 31 * result + (name != null ? name.hashCode() : 0); 167 return result; 168 } 169 170 @Override 171 public String toString() { 172 return getAbsolutePath(); 173 } 174 175 public abstract boolean canRead(); 176 177 public abstract boolean canWrite(); 178 179 public abstract boolean createNewFile() throws IOException; 180 181 public abstract boolean delete(); 182 183 public abstract void deleteOnExit(); 184 185 public abstract boolean exists(); 186 187 public abstract String getName(); 188 189 public abstract boolean isDirectory(); 190 191 public abstract boolean isFile(); 192 193 public abstract boolean isHidden(); 194 195 public abstract long lastModified(); 196 197 public abstract long length(); 198 199 public abstract boolean mkdir(); 200 201 public abstract boolean mkdirs(); 202 203 public abstract boolean renameTo(PseudoFile dest); 204 205 public abstract boolean setLastModified(long time); 206 207 abstract InputStream $newInputStream() throws IOException; 208 209 abstract OutputStream $newOutputStream() throws IOException; 210 211 abstract OutputStream $newOutputStream(boolean append) throws IOException; 212}