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