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