001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.res.type.ram; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.List; 024 025/** 026 * Core of a Ram Resource, holds the concrete data for a existing resource 027 */ 028public final class RamResourceCore implements Serializable { 029 030 /** 031 * Directory Resource 032 */ 033 public static final int TYPE_DIRECTORY=1; 034 035 /** 036 * Directory Resource 037 */ 038 public static final int TYPE_FILE=2; 039 040 private static final String[] EMPTY_NAMES = new String[0]; 041 042 private int type; 043 private String name; 044 private byte[] data; 045 private List children; 046 private long lastModified=System.currentTimeMillis(); 047 048 private int mode=0777; 049 private int attributes=0; 050 051 052 private RamResourceCore parent; 053 054 /** 055 * Konstruktor 056 * @param parent 057 * @param type 058 * @param name 059 * @param caseSensitive 060 */ 061 public RamResourceCore(RamResourceCore parent, int type,String name) { 062 if(parent!=null) { 063 parent.addChild(this); 064 } 065 this.parent=parent; 066 this.type=type; 067 this.name=name; 068 } 069 070 071 /** 072 * Gibt den Feldnamen lastModified zurueck. 073 * @return lastModified 074 */ 075 public long getLastModified() { 076 return this.lastModified; 077 } 078 079 /** 080 * Setzt den Feldnamen lastModified. 081 * @param lastModified lastModified 082 */ 083 public void setLastModified(long lastModified) { 084 this.lastModified = lastModified; 085 } 086 087 /** 088 * Gibt den Feldnamen children zurueck. 089 * @return children 090 */ 091 public String[] getChildNames() { 092 if(children==null || children.size()==0) return EMPTY_NAMES; 093 String[] arr = new String[children.size()]; 094 for(int i=0;i<arr.length;i++) { 095 arr[i]=((RamResourceCore)children.get(i)).getName(); 096 } 097 return arr; 098 } 099 100 /** 101 * Setzt den Feldnamen children. 102 * @param children children 103 */ 104 public void setChildren(List children) { 105 this.children = children; 106 } 107 108 /** 109 * Gibt den Feldnamen data zurueck. 110 * @return data 111 */ 112 public byte[] getData() { 113 return this.data; 114 } 115 116 /** 117 * Setzt den Feldnamen data. 118 * @param data data 119 * @param append 120 */ 121 public void setData(byte[] data,boolean append) { 122 lastModified=System.currentTimeMillis(); 123 124 // set data 125 if(append) { 126 if(this.data!=null && data!=null) { 127 byte[] newData=new byte[this.data.length+data.length]; 128 int i=0; 129 for(;i<this.data.length;i++) { 130 newData[i]=this.data[i]; 131 } 132 for(;i<this.data.length+data.length;i++) { 133 newData[i]=data[i-this.data.length]; 134 } 135 this.data=newData; 136 } 137 else if(data!=null) { 138 this.data=data; 139 } 140 } 141 else { 142 this.data=data; 143 } 144 145 // set type 146 if(this.data!=null) this.type=RamResourceCore.TYPE_FILE; 147 148 } 149 150 /** 151 * Gibt den Feldnamen name zurueck. 152 * @return name 153 */ 154 public String getName() { 155 return this.name; 156 } 157 158 /** 159 * Setzt den Feldnamen name. 160 * @param name name 161 */ 162 public void setName(String name) { 163 lastModified=System.currentTimeMillis(); 164 this.name = name; 165 } 166 167 /** 168 * Gibt den Feldnamen type zurueck. 169 * @return type 170 */ 171 public int getType() { 172 return this.type; 173 } 174 175 /** 176 * Setzt den Feldnamen type. 177 * @param type type 178 */ 179 public void setType(int type) { 180 lastModified=System.currentTimeMillis(); 181 this.type = type; 182 } 183 184 public void addChild(RamResourceCore child) { 185 if(children==null)children=new ArrayList(); 186 children.add(child); 187 } 188 189 /** 190 * returns a child that match given name 191 * @param name 192 * @return matching child 193 */ 194 public RamResourceCore getChild(String name, boolean caseSensitive) { 195 if(children==null) return null; 196 197 RamResourceCore child; 198 for(int i=children.size()-1;i>=0;i--) { 199 child=(RamResourceCore) children.get(i); 200 if(child!=null 201 && (caseSensitive?child.getName().equals(name):child.getName().equalsIgnoreCase(name))) 202 return child; 203 } 204 return null; 205 } 206 207 /** 208 * returns the parent if this core 209 * @return parent core or null if no parent available 210 */ 211 public RamResourceCore getParent() { 212 return parent; 213 } 214 215 /** 216 * remove given child from this core 217 * @param core 218 */ 219 public void removeChild(RamResourceCore core) { 220 221 if(children==null) return; 222 223 RamResourceCore child; 224 for(int i=children.size()-1;i>=0;i--) { 225 child=(RamResourceCore) children.get(i); 226 if(child==core) { 227 children.remove(i); 228 break; 229 } 230 } 231 } 232 /** 233 * @return the mode 234 */ 235 public int getMode() { 236 return mode; 237 } 238 239 /** 240 * @param mode the mode to set 241 */ 242 public void setMode(int mode) { 243 this.mode=mode; 244 } 245 246 public int getAttributes() { 247 return attributes; 248 } 249 250 public void setAttributes(int attributes) { 251 this.attributes=attributes; 252 } 253 254 public void remove() { 255 setType(0); 256 setData(null,false); 257 setChildren(null); 258 RamResourceCore p=getParent(); 259 if(p!=null)p.removeChild(this); 260 } 261 262}