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