1 /*-
2  * Copyright (C) 2008 Erik Larsson
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 package org.catacombae.hfs.types.hfscommon;
19 
20 import java.io.PrintStream;
21 import org.catacombae.csjc.structelements.Dictionary;
22 import org.catacombae.hfs.types.hfsplus.HFSPlusCatalogKey;
23 import org.catacombae.hfs.types.hfsplus.HFSPlusCatalogFile;
24 import org.catacombae.hfs.types.hfs.CatKeyRec;
25 import org.catacombae.hfs.types.hfs.CdrFilRec;
26 
27 /**
28  * @author <a href="http://www.catacombae.org/" target="_top">Erik Larsson</a>
29  */
30 public abstract class CommonHFSCatalogFileRecord extends CommonHFSCatalogLeafRecord {
31     protected final CommonHFSCatalogKey key;
32     protected final CommonHFSCatalogFile data;
33 
CommonHFSCatalogFileRecord(CommonHFSCatalogKey key, CommonHFSCatalogFile data)34     private CommonHFSCatalogFileRecord(CommonHFSCatalogKey key,
35             CommonHFSCatalogFile data) {
36         this.key = key;
37         this.data = data;
38     }
39 
40     @Override
getKey()41     public CommonHFSCatalogKey getKey() {
42         return key;
43     }
44 
getData()45     public CommonHFSCatalogFile getData() {
46         return data;
47     }
48 
49     /* @Override */
print(PrintStream ps, String prefix)50     public void print(PrintStream ps, String prefix) {
51         ps.println(prefix + CommonHFSCatalogFileRecord.class.getSimpleName() + ":");
52         printFields(ps, prefix + " ");
53     }
54 
printFields(PrintStream ps, String prefix)55     public void printFields(PrintStream ps, String prefix) {
56         ps.println(prefix + "key:");
57         key.print(ps, prefix + " ");
58         ps.println(prefix + "data:");
59         data.print(ps, prefix + " ");
60     }
61 
62     /* @Override */
getStructElements()63     public Dictionary getStructElements() {
64         DictionaryBuilder db =
65                 new DictionaryBuilder(CommonHFSCatalogFileRecord.class.getSimpleName(),
66                 "File record");
67 
68         db.add("key", key.getStructElements(), "Catalog key");
69         db.add("data", data.getStructElements(), "File data");
70 
71         return db.getResult();
72     }
73 
create(HFSPlusCatalogKey key, HFSPlusCatalogFile data)74     public static CommonHFSCatalogFileRecord create(HFSPlusCatalogKey key,
75             HFSPlusCatalogFile data) {
76         return new HFSPlusImplementation(key, data);
77     }
78 
create(CatKeyRec key, CdrFilRec data)79     public static CommonHFSCatalogFileRecord create(CatKeyRec key, CdrFilRec data) {
80         return new HFSImplementation(key, data);
81     }
82 
83     public static class HFSImplementation extends CommonHFSCatalogFileRecord {
HFSImplementation(CatKeyRec key, CdrFilRec data)84         public HFSImplementation(CatKeyRec key, CdrFilRec data) {
85             super(CommonHFSCatalogKey.create(key), CommonHFSCatalogFile.create(data));
86         }
87 
HFSImplementation(CommonHFSCatalogKey key, CommonHFSCatalogFile data)88         protected HFSImplementation(CommonHFSCatalogKey key,
89             CommonHFSCatalogFile data) {
90             super(key, data);
91         }
92 
93         @Override
getSize()94         public int getSize() {
95             return key.occupiedSize() + data.size();
96         }
97 
98         @Override
getBytes()99         public byte[] getBytes() {
100             byte[] result = new byte[getSize()];
101             byte[] tempData;
102             int offset = 0;
103 
104             tempData = key.getBytes();
105             System.arraycopy(tempData, 0, result, offset, tempData.length); offset += tempData.length;
106             tempData = data.getBytes();
107             System.arraycopy(tempData, 0, result, offset, tempData.length); offset += tempData.length;
108 
109             return result;
110         }
111     }
112 
113     public static class HFSPlusImplementation extends HFSImplementation {
HFSPlusImplementation(HFSPlusCatalogKey key, HFSPlusCatalogFile data)114         public HFSPlusImplementation(HFSPlusCatalogKey key, HFSPlusCatalogFile data) {
115             super(CommonHFSCatalogKey.create(key), CommonHFSCatalogFile.create(data));
116         }
117     }
118 }
119