Initial import.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / CompressSection.java
CommitLineData
878ddf1f 1/** @file\r
2 CompressSection class.\r
3\r
4 CompressSection indicate that all section which in it should be compressed.\r
5 \r
6 Copyright (c) 2006, Intel Corporation\r
7 All rights reserved. This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11 \r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15 **/\r
16\r
17\r
18package org.tianocore.framework.tasks;\r
19\r
20import java.io.DataInputStream;\r
21import java.io.DataOutputStream;\r
22import java.io.File;\r
23import java.io.FileInputStream;\r
24import java.io.FileOutputStream;\r
25import java.util.ArrayList;\r
26import java.util.Iterator;\r
27import java.util.List;\r
28\r
29import org.apache.tools.ant.BuildException;\r
30\r
31\r
32/**\r
33 CompressSection\r
34 \r
35 CompressSection indicate that all section which in it should be compressed. \r
36 \r
37**/\r
38public class CompressSection implements Section, FfsTypes{\r
39 //\r
40 // The attribute of compressName.\r
41 //\r
42 String compressName = "";\r
43 //\r
44 // The list contained the SectFile element.\r
45 //\r
46 List<Object> SectList = new ArrayList<Object>();\r
47\r
48 /**\r
49 toBuffer\r
50 \r
51 This function is to collect all sectFile and compress it , then output\r
52 the result to buffer.\r
53 \r
54 @param Buffer The point of output buffer\r
55 \r
56 **/\r
57 public void toBuffer (DataOutputStream Buffer){\r
58 \r
59 Section sect;\r
60 File compressOut;\r
61 \r
62 //\r
63 // Get section file in compress node.\r
64 //\r
65 try{\r
66 compressOut = new File ("Compress.temp");\r
67 FileOutputStream fo = new FileOutputStream (compressOut.getName());\r
68 DataOutputStream Do = new DataOutputStream (fo);\r
69 \r
70 //\r
71 // Get each section which under the compress {};\r
72 // And add it is contains to File;\r
73 //\r
74 Iterator SectionIter = SectList.iterator();\r
75 while (SectionIter.hasNext()){\r
76 sect = (Section)SectionIter.next();\r
77 \r
78 //\r
79 // Call each section class's toBuffer function.\r
80 //\r
81 try {\r
82 sect.toBuffer(Do);\r
83 }\r
84 catch (BuildException e) {\r
85 System.out.print(e.getMessage());\r
86 throw new BuildException ("Compress.toBuffer failed at section");\r
87 } \r
88 \r
89 }\r
90 Do.close(); \r
91 \r
92 //\r
93 // Get contain to Buffer\r
94 //\r
95 FileInputStream fi = new FileInputStream (compressOut.getName());\r
96 DataInputStream di = new DataInputStream (fi);\r
97 byte[] fileBuffer = new byte[(int)compressOut.length()];\r
98 di.read(fileBuffer);\r
99 \r
100 //\r
101 // Call compress\r
102 //\r
103 Compress myCompress = new Compress(fileBuffer, fileBuffer.length); \r
104 \r
105 //\r
106 // Add Compress header\r
107 //\r
108 CompressHeader Ch = new CompressHeader();\r
109 Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +\r
110 Ch.GetSize()) &\r
111 0xff\r
112 );\r
113 Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length + \r
114 Ch.GetSize())&\r
115 0xff00) >> 8\r
116 );\r
117 Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length + \r
118 Ch.GetSize()) & \r
119 0xff0000) >> 16\r
120 );\r
121 Ch.SectionHeader.type = (byte) EFI_SECTION_COMPRESSION;\r
122 \r
123 //\r
124 // Note: The compressName was not effective now. Using the\r
125 // EFI_STANDARD_COMPRSSION for compressType .\r
126 // That is follow old Genffsfile tools. Some code will be added for \r
127 // the different compressName;\r
128 //\r
129 Ch.UncompressLen = fileBuffer.length;\r
130 Ch.CompressType = EFI_STANDARD_COMPRESSION; \r
131 \r
132 //\r
133 // Change header struct to byte buffer\r
134 //\r
135 byte [] headerBuffer = new byte[Ch.GetSize()];\r
136 Ch.StructToBuffer(headerBuffer);\r
137 \r
138 //\r
139 // First add CompressHeader to Buffer, then add Compress data.\r
140 //\r
141 Buffer.write (headerBuffer);\r
142 Buffer.write(myCompress.outputBuffer); \r
143 \r
144 //\r
145 // 4 Byte aligment \r
146 //\r
147 int size = Ch.GetSize() + myCompress.outputBuffer.length;\r
148 \r
149 while ((size & 0x03) != 0){\r
150 size ++;\r
151 Buffer.writeByte(0);\r
152 }\r
153 \r
154 //\r
155 // Delete temp file\r
156 //\r
157 di.close();\r
158 compressOut.delete();\r
159 \r
160 }\r
161 catch (Exception e){\r
162 throw new BuildException("compress.toBuffer failed!\n");\r
163 } \r
164 }\r
165\r
166 /**\r
167 getCompressName\r
168 \r
169 This function is to get compressName.\r
170 \r
171 @return The compressName.\r
172 **/\r
173 public String getCompressName() {\r
174 return compressName;\r
175 }\r
176\r
177 /**\r
178 setCompressName\r
179 \r
180 This function is to set compressName.\r
181 \r
182 @param compressName The string of compressName\r
183 **/\r
184 public void setCompressName(String compressName) {\r
185 this.compressName = compressName;\r
186 }\r
187\r
188 /**\r
189 addSectFile\r
190 \r
191 This function is to add sectFile element to SectList.\r
192 \r
193 @param sectFile SectFile element which succeed from section class.\r
194 **/\r
195 public void addSectFile (SectFile sectFile) {\r
196 SectList.add(sectFile);\r
197 \r
198 } \r
199 \r
200 /**\r
201 addTool\r
202 \r
203 This function is to add tool element to SectList.\r
204 @param tool Tool element which succeed from section class.\r
205 **/\r
206 public void addTool (Tool tool) {\r
207 SectList.add(tool);\r
208 }\r
209}