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