- 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 / SectFile.java
1 /** @file
2 This file is to define nested element which is meant for specifying section file
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14 package org.tianocore.framework.tasks;
15
16 import java.io.DataInputStream;
17 import java.io.DataOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import org.apache.tools.ant.BuildException;
21
22 /**
23 Class SectFile is to define a class corresponding to ANT nested element. It's
24 used to specify section file(s) when used with GenFfsFile task
25 **/
26 public class SectFile implements Section {
27 private String fileName = ""; /// section file name
28 private int alignment = 0;
29
30 /**
31 Get method of ANT task/datatype for "FileName" attribute
32
33 @returns The name of section file
34 **/
35 public String getFileName() {
36 return fileName;
37 }
38
39 /**
40 Set method of ANT task/datatype for "FileName" attribute
41
42 @param fileName The name of section file
43 **/
44 public void setFileName(String fileName) {
45 this.fileName = fileName;
46 }
47
48 public int getAlignment() {
49 return alignment;
50 }
51
52 public void setAlignment(int alignment) {
53 if (alignment > 7) {
54 this.alignment = 7;
55 } else {
56 this.alignment = alignment;
57 }
58 }
59
60 public SectFile (){
61 }
62
63 /**
64 Put the content of section file into specified buffer with extra bytes for
65 alignment requirement.
66
67 @param Buffer buffer to contain the section file content with alignment
68 **/
69 public void toBuffer (DataOutputStream buffer){
70 File sectFile;
71 int fileLen;
72
73 ///
74 /// open file
75 ///
76 sectFile = new File (this.fileName);
77
78 ///
79 /// check if file exist.
80 ///
81 if (! sectFile.exists()) {
82 throw new BuildException("The file " + this.fileName + " is not exist!\n");
83 }
84
85
86 ///
87 /// Read section file and add it's contains to buffer.
88 ///
89 try {
90
91 FileInputStream fs = new FileInputStream (sectFile.getAbsoluteFile());
92 DataInputStream In = new DataInputStream (fs);
93 fileLen = (int)sectFile.length();
94 byte[] sectBuffer = new byte[fileLen];
95 In.read(sectBuffer);
96 buffer.write(sectBuffer);
97
98 ///
99 /// 4 byte alignment
100 ///
101 while ((fileLen & 0x03)!= 0) {
102 fileLen ++;
103 buffer.writeByte(0);
104 }
105
106 ///
107 /// close inputStream
108 ///
109 In.close();
110
111 } catch (Exception e) {
112 System.out.print(e.getMessage());
113 throw new BuildException("SectFile, toBuffer failed!\n");
114 }
115 }
116 }