Initial import.
[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){
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);
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 effective 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 // 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 //
155 // Delete temp file
156 //
157 di.close();
158 compressOut.delete();
159
160 }
161 catch (Exception e){
162 throw new BuildException("compress.toBuffer failed!\n");
163 }
164 }
165
166 /**
167 getCompressName
168
169 This function is to get compressName.
170
171 @return The compressName.
172 **/
173 public String getCompressName() {
174 return compressName;
175 }
176
177 /**
178 setCompressName
179
180 This function is to set compressName.
181
182 @param compressName The string of compressName
183 **/
184 public void setCompressName(String compressName) {
185 this.compressName = compressName;
186 }
187
188 /**
189 addSectFile
190
191 This function is to add sectFile element to SectList.
192
193 @param sectFile SectFile element which succeed from section class.
194 **/
195 public void addSectFile (SectFile sectFile) {
196 SectList.add(sectFile);
197
198 }
199
200 /**
201 addTool
202
203 This function is to add tool element to SectList.
204 @param tool Tool element which succeed from section class.
205 **/
206 public void addTool (Tool tool) {
207 SectList.add(tool);
208 }
209 }