- Fixed EDKT240. Now the Blank.pad file for alignment purpose will no longer be needed.
[mirror_edk2.git] / Tools / Java / 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
87379bbe 20import java.io.ByteArrayOutputStream;\r
878ddf1f 21import java.io.DataOutputStream;\r
878ddf1f 22import java.util.ArrayList;\r
23import java.util.Iterator;\r
24import java.util.List;\r
25\r
26import org.apache.tools.ant.BuildException;\r
27\r
28\r
29/**\r
30 CompressSection\r
31 \r
32 CompressSection indicate that all section which in it should be compressed. \r
33 \r
34**/\r
93f5dd0a 35public class CompressSection implements Section, FfsTypes {\r
e3cc4061 36 private int alignment = 0;\r
878ddf1f 37 //\r
38 // The attribute of compressName.\r
39 //\r
a1ffb10f 40 private String compressName = "";\r
878ddf1f 41 //\r
42 // The list contained the SectFile element.\r
43 //\r
a1ffb10f 44 private List<Section> sectList = new ArrayList<Section>();\r
878ddf1f 45\r
2eb7d78d 46 public static Object semaphore = new Object();\r
878ddf1f 47 /**\r
48 toBuffer\r
49 \r
50 This function is to collect all sectFile and compress it , then output\r
51 the result to buffer.\r
52 \r
53 @param Buffer The point of output buffer\r
54 \r
55 **/\r
87379bbe 56 public void toBuffer (DataOutputStream buffer){\r
878ddf1f 57 \r
58 Section sect;\r
878ddf1f 59 \r
60 //\r
61 // Get section file in compress node.\r
62 //\r
63 try{\r
87379bbe 64 \r
65 ByteArrayOutputStream bo = new ByteArrayOutputStream ();\r
66 DataOutputStream Do = new DataOutputStream (bo);\r
878ddf1f 67 \r
68 //\r
69 // Get each section which under the compress {};\r
70 // And add it is contains to File;\r
71 //\r
a1ffb10f 72 Iterator SectionIter = sectList.iterator();\r
878ddf1f 73 while (SectionIter.hasNext()){\r
74 sect = (Section)SectionIter.next();\r
75 \r
76 //\r
77 // Call each section class's toBuffer function.\r
78 //\r
79 try {\r
87379bbe 80 sect.toBuffer(Do);\r
878ddf1f 81 }\r
82 catch (BuildException e) {\r
83 System.out.print(e.getMessage());\r
84 throw new BuildException ("Compress.toBuffer failed at section");\r
85 } \r
86 \r
87 }\r
88 Do.close(); \r
89 \r
2eb7d78d 90 synchronized (semaphore) {\r
878ddf1f 91 //\r
92 // Call compress\r
93 //\r
87379bbe 94 byte[] fileBuffer = bo.toByteArray();\r
878ddf1f 95 Compress myCompress = new Compress(fileBuffer, fileBuffer.length); \r
96 \r
97 //\r
98 // Add Compress header\r
99 //\r
100 CompressHeader Ch = new CompressHeader();\r
101 Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +\r
102 Ch.GetSize()) &\r
103 0xff\r
104 );\r
105 Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length + \r
106 Ch.GetSize())&\r
107 0xff00) >> 8\r
108 );\r
109 Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length + \r
110 Ch.GetSize()) & \r
111 0xff0000) >> 16\r
112 );\r
113 Ch.SectionHeader.type = (byte) EFI_SECTION_COMPRESSION;\r
114 \r
115 //\r
2da8968b 116 // Note: The compressName was not efsfective now. Using the\r
878ddf1f 117 // EFI_STANDARD_COMPRSSION for compressType .\r
118 // That is follow old Genffsfile tools. Some code will be added for \r
119 // the different compressName;\r
120 //\r
121 Ch.UncompressLen = fileBuffer.length;\r
122 Ch.CompressType = EFI_STANDARD_COMPRESSION; \r
123 \r
124 //\r
125 // Change header struct to byte buffer\r
126 //\r
127 byte [] headerBuffer = new byte[Ch.GetSize()];\r
128 Ch.StructToBuffer(headerBuffer);\r
129 \r
130 //\r
131 // First add CompressHeader to Buffer, then add Compress data.\r
132 //\r
2da8968b 133 buffer.write (headerBuffer);\r
134 buffer.write(myCompress.outputBuffer); \r
878ddf1f 135 \r
136 //\r
2da8968b 137 // Buffer 4 Byte aligment \r
878ddf1f 138 //\r
139 int size = Ch.GetSize() + myCompress.outputBuffer.length;\r
140 \r
141 while ((size & 0x03) != 0){\r
142 size ++;\r
2da8968b 143 buffer.writeByte(0);\r
144 }\r
145 //\r
878ddf1f 146 // Delete temp file\r
147 //\r
87379bbe 148 //di.close();\r
149 //compressOut.delete();\r
2eb7d78d 150 }\r
878ddf1f 151 \r
152 }\r
153 catch (Exception e){\r
154 throw new BuildException("compress.toBuffer failed!\n");\r
155 } \r
156 }\r
157\r
158 /**\r
159 getCompressName\r
160 \r
161 This function is to get compressName.\r
162 \r
163 @return The compressName.\r
164 **/\r
165 public String getCompressName() {\r
166 return compressName;\r
167 }\r
168\r
169 /**\r
170 setCompressName\r
171 \r
172 This function is to set compressName.\r
173 \r
174 @param compressName The string of compressName\r
175 **/\r
176 public void setCompressName(String compressName) {\r
177 this.compressName = compressName;\r
178 }\r
179\r
180 /**\r
181 addSectFile\r
182 \r
183 This function is to add sectFile element to SectList.\r
184 \r
185 @param sectFile SectFile element which succeed from section class.\r
186 **/\r
187 public void addSectFile (SectFile sectFile) {\r
a1ffb10f 188 sectList.add(sectFile);\r
878ddf1f 189 \r
190 } \r
191 \r
192 /**\r
193 addTool\r
194 \r
195 This function is to add tool element to SectList.\r
196 @param tool Tool element which succeed from section class.\r
197 **/\r
198 public void addTool (Tool tool) {\r
a1ffb10f 199 sectList.add(tool);\r
878ddf1f 200 }\r
e3cc4061 201\r
202 public int getAlignment() {\r
203 return alignment;\r
204 }\r
205\r
206 public void setAlignment(int alignment) {\r
207 if (alignment > 7) {\r
208 this.alignment = 7;\r
209 } else {\r
210 this.alignment = alignment;\r
211 }\r
212 }\r
878ddf1f 213}