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.cache;
020
021import java.io.Serializable;
022
023/**
024 * Core of a Ram Resource, holds the concrete data for a existing resource
025 */
026public final class CacheResourceCore implements Serializable {
027
028
029        /**
030         * Directory Resource
031         */
032        public static final int TYPE_DIRECTORY=1;
033
034        /**
035         * Directory Resource
036         */
037        public static final int TYPE_FILE=2;
038
039        
040        private int type;
041        private String name;
042        private byte[] data;
043        private long lastModified=System.currentTimeMillis();
044
045        private int mode=0777;
046        private int attributes=0;
047
048        private String path;
049        
050        /**
051         * Konstruktor
052         * @param parent
053         * @param type
054         * @param name
055         * @param caseSensitive 
056         */
057        public CacheResourceCore(int type,String path,String name) {
058                this.type=type;
059                this.path=path;
060                this.name=name;
061        }
062
063
064        /**
065         * Gibt den Feldnamen lastModified zurueck.
066         * @return lastModified
067         */
068        public long getLastModified() {
069                return this.lastModified;
070        }
071
072        /**
073         * Setzt den Feldnamen lastModified.
074         * @param lastModified lastModified 
075         */
076        public void setLastModified(long lastModified) {
077                this.lastModified = lastModified;
078        }
079
080        /**
081         * Gibt den Feldnamen data zurueck.
082         * @return data
083         */
084        public byte[] getData() {
085                return this.data;
086        }
087
088        /**
089         * Setzt den Feldnamen data.
090         * @param data data 
091         * @param append 
092         */
093        public void setData(byte[] data,boolean append) {
094                lastModified=System.currentTimeMillis();
095                
096                // set data
097                if(append) {
098                        if(this.data!=null && data!=null) {
099                                byte[] newData=new byte[this.data.length+data.length];
100                                int i=0;
101                                for(;i<this.data.length;i++) {
102                                        newData[i]=this.data[i];
103                                }
104                                for(;i<this.data.length+data.length;i++) {
105                                        newData[i]=data[i-this.data.length];
106                                }
107                                this.data=newData;
108                        }
109                        else if(data!=null) {
110                                this.data=data;
111                        }
112                }
113                else {
114                        this.data=data;
115                }
116                
117                // set type
118                if(this.data!=null) this.type=TYPE_FILE;
119                
120        }
121
122        /**
123         * Gibt den Feldnamen name zurueck.
124         * @return name
125         */
126        public String getName() {
127                return this.name;
128        }
129
130        /**
131         * Setzt den Feldnamen name.
132         * @param name name 
133         */
134        public void setName(String name) {
135                lastModified=System.currentTimeMillis();
136                this.name = name;
137        }
138        
139
140        /**
141         * @return the path
142         */
143        public String getPath() {
144                return path;
145        }
146
147        /**
148         * Gibt den Feldnamen type zurueck.
149         * @return type
150         */
151        public int getType() {
152                return this.type;
153        }
154
155        /**
156         * Setzt den Feldnamen type.
157         * @param type type 
158         */
159        public void setType(int type) {
160                lastModified=System.currentTimeMillis();
161                this.type = type;
162        }
163        
164        /**
165         * @return the mode
166         */
167        public int getMode() {
168                return mode;
169        }
170
171        /**
172         * @param mode the mode to set
173         */
174        public void setMode(int mode) {
175                this.mode=mode;
176        }
177        
178        public int getAttributes() {
179                return attributes;
180        }
181        
182        public void setAttributes(int attributes) {
183                this.attributes=attributes;
184        }
185
186        public void remove() {
187                setType(0);
188                setData(null,false);
189        }
190
191}