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