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