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 |
e3cc4061 |
28 | private int alignment = 0;\r |
878ddf1f |
29 | \r |
30 | /**\r |
31 | Get method of ANT task/datatype for "FileName" attribute\r |
32 | \r |
33 | @returns The name of section file\r |
34 | **/\r |
35 | public String getFileName() {\r |
36 | return fileName;\r |
37 | }\r |
38 | \r |
39 | /**\r |
40 | Set method of ANT task/datatype for "FileName" attribute\r |
41 | \r |
42 | @param fileName The name of section file\r |
43 | **/\r |
44 | public void setFileName(String fileName) {\r |
45 | this.fileName = fileName; \r |
46 | }\r |
47 | \r |
e3cc4061 |
48 | public int getAlignment() {\r |
49 | return alignment;\r |
50 | }\r |
51 | \r |
52 | public void setAlignment(int alignment) {\r |
53 | if (alignment > 7) {\r |
54 | this.alignment = 7;\r |
55 | } else {\r |
56 | this.alignment = alignment;\r |
57 | }\r |
58 | }\r |
59 | \r |
878ddf1f |
60 | public SectFile (){\r |
61 | }\r |
62 | \r |
63 | /**\r |
64 | Put the content of section file into specified buffer with extra bytes for \r |
65 | alignment requirement.\r |
66 | \r |
67 | @param Buffer buffer to contain the section file content with alignment\r |
68 | **/\r |
87379bbe |
69 | public void toBuffer (DataOutputStream buffer){\r |
878ddf1f |
70 | File sectFile;\r |
87379bbe |
71 | int fileLen;\r |
878ddf1f |
72 | \r |
73 | ///\r |
74 | /// open file\r |
75 | ///\r |
76 | sectFile = new File (this.fileName); \r |
77 | \r |
78 | ///\r |
79 | /// check if file exist.\r |
80 | /// \r |
81 | if (! sectFile.exists()) {\r |
87379bbe |
82 | throw new BuildException("The file " + this.fileName + " is not exist!\n"); \r |
878ddf1f |
83 | }\r |
84 | \r |
85 | \r |
86 | ///\r |
87 | /// Read section file and add it's contains to buffer.\r |
88 | ///\r |
89 | try {\r |
90 | \r |
91 | FileInputStream fs = new FileInputStream (sectFile.getAbsoluteFile());\r |
92 | DataInputStream In = new DataInputStream (fs);\r |
87379bbe |
93 | fileLen = (int)sectFile.length();\r |
94 | byte[] sectBuffer = new byte[fileLen];\r |
95 | In.read(sectBuffer);\r |
96 | buffer.write(sectBuffer);\r |
878ddf1f |
97 | \r |
98 | ///\r |
99 | /// 4 byte alignment\r |
100 | ///\r |
101 | while ((fileLen & 0x03)!= 0) {\r |
102 | fileLen ++;\r |
2da8968b |
103 | buffer.writeByte(0);\r |
878ddf1f |
104 | } \r |
105 | \r |
106 | ///\r |
107 | /// close inputStream\r |
108 | ///\r |
109 | In.close();\r |
110 | \r |
111 | } catch (Exception e) {\r |
112 | System.out.print(e.getMessage());\r |
3f7b510e |
113 | throw new BuildException("SectFile, toBuffer failed!\n"); \r |
878ddf1f |
114 | }\r |
115 | }\r |
87379bbe |
116 | } |