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