b603a3eb80a92b84af3af0e0475796d4d78f395d
[mirror_edk2.git] / Tools / 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
29 /**
30 Get method of ANT task/datatype for "FileName" attribute
31
32 @returns The name of section file
33 **/
34 public String getFileName() {
35 return fileName;
36 }
37
38 /**
39 Set method of ANT task/datatype for "FileName" attribute
40
41 @param fileName The name of section file
42 **/
43 public void setFileName(String fileName) {
44 this.fileName = fileName;
45 }
46
47 public SectFile (){
48 }
49
50 /**
51 Put the content of section file into specified buffer with extra bytes for
52 alignment requirement.
53
54 @param Buffer buffer to contain the section file content with alignment
55 **/
56 public void toBuffer (DataOutputStream buffer, DataOutputStream orgBuffer){
57 File sectFile;
58 byte data;
59 long fileLen;
60
61 ///
62 /// open file
63 ///
64 sectFile = new File (this.fileName);
65
66 ///
67 /// check if file exist.
68 ///
69 if (! sectFile.exists()) {
70 throw new BuildException("The file " + this.fileName + " does not exist!\n");
71 }
72
73
74 ///
75 /// Read section file and add it's contains to buffer.
76 ///
77 try {
78
79 FileInputStream fs = new FileInputStream (sectFile.getAbsoluteFile());
80 DataInputStream In = new DataInputStream (fs);
81 fileLen = sectFile.length();
82
83 int i = 0;
84 while (i < fileLen) {
85 data = In.readByte();
86 buffer.writeByte(data);
87 //
88 // Add data to org file
89 //
90 orgBuffer.writeByte(data);
91 i++;
92 }
93
94 ///
95 /// 4 byte alignment
96 ///
97 while ((fileLen & 0x03)!= 0) {
98 fileLen ++;
99 buffer.writeByte(0);
100 //
101 // Add data to org file
102 //
103 orgBuffer.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 }