c110f3bc50b27f8e6cfd34bca66a7ee654daf8ab
[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
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){
57 File sectFile;
58 int fileLen;
59
60 ///
61 /// open file
62 ///
63 sectFile = new File (this.fileName);
64
65 ///
66 /// check if file exist.
67 ///
68 if (! sectFile.exists()) {
69 throw new BuildException("The file " + this.fileName + " is not exist!\n");
70 }
71
72
73 ///
74 /// Read section file and add it's contains to buffer.
75 ///
76 try {
77
78 FileInputStream fs = new FileInputStream (sectFile.getAbsoluteFile());
79 DataInputStream In = new DataInputStream (fs);
80 fileLen = (int)sectFile.length();
81 byte[] sectBuffer = new byte[fileLen];
82 In.read(sectBuffer);
83 buffer.write(sectBuffer);
84
85 ///
86 /// 4 byte alignment
87 ///
88 while ((fileLen & 0x03)!= 0) {
89 fileLen ++;
90 buffer.writeByte(0);
91 }
92
93 ///
94 /// close inputStream
95 ///
96 In.close();
97
98 } catch (Exception e) {
99 System.out.print(e.getMessage());
100 throw new BuildException("SectFile, toBuffer failed!\n");
101 }
102 }
103 }