AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / authz-core / src / main / java / org / onap / aaf / authz / local / DataFile.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.authz.local;\r
24 \r
25 import java.io.File;\r
26 import java.io.FileNotFoundException;\r
27 import java.io.IOException;\r
28 import java.io.RandomAccessFile;\r
29 import java.nio.ByteBuffer;\r
30 import java.nio.IntBuffer;\r
31 import java.nio.MappedByteBuffer;\r
32 import java.nio.channels.FileChannel;\r
33 import java.nio.channels.FileChannel.MapMode;\r
34 \r
35 public class DataFile {\r
36         private RandomAccessFile rafile;\r
37         private FileChannel channel;\r
38         public MappedByteBuffer mapBuff;\r
39         private final File file;\r
40         private final String access;\r
41         \r
42         public DataFile(File file, String access)  {\r
43                 this.file = file;\r
44                 this.access = access;\r
45         }\r
46         public void open() throws IOException {\r
47                 if(!file.exists()) throw new FileNotFoundException();\r
48                 rafile = new RandomAccessFile(file,access);\r
49                 channel = rafile.getChannel();\r
50                 mapBuff = channel.map("r".equals(access)?MapMode.READ_ONLY:MapMode.READ_WRITE,0,channel.size());\r
51         }\r
52         public void close() throws IOException {\r
53                 if(channel!=null){channel.close();}\r
54                 if(rafile!=null) {rafile.close();}\r
55                 mapBuff = null;\r
56         }\r
57 \r
58         public long size() throws IOException {\r
59                 return channel.size();\r
60         }\r
61 \r
62         private synchronized int load(Token t) {\r
63                 int len = Math.min(mapBuff.limit()-t.next,t.buff.length);\r
64                 if(len>0) {\r
65                         mapBuff.position(t.next);\r
66                         mapBuff.get(t.buff,0,len);\r
67                 }\r
68                 return len<0?0:len;\r
69         }\r
70         \r
71         public class Token {\r
72                 private byte[] buff;\r
73                 int pos, next, end;\r
74                 \r
75                 public Token(int size) {\r
76                         buff = new byte[size];\r
77                         pos = next = end = 0;\r
78                 }\r
79                 \r
80                 public boolean pos(int to) {\r
81                         pos = next = to;\r
82                         return (end=load(this))>0;\r
83                 }\r
84                 \r
85                 public boolean nextLine() {\r
86                         end = load(this);\r
87                         pos = next;\r
88                         for(int i=0;i<end;++i) {\r
89                                 if(buff[i]=='\n') {\r
90                                         end = i;\r
91                                         next += i+1;\r
92                                         return true;\r
93                                 }\r
94                         }\r
95                         return false;\r
96                 }\r
97                 \r
98                 public IntBuffer getIntBuffer() {\r
99                         return ByteBuffer.wrap(buff).asIntBuffer();\r
100                 }\r
101 \r
102 \r
103 \r
104                 public String toString() {\r
105                         return new String(buff,0,end);\r
106                 }\r
107                 public class Field {\r
108                         char delim;\r
109                         int idx;\r
110                         ByteBuffer bb;\r
111 \r
112                         public Field(char delimiter) {\r
113                                 delim = delimiter;\r
114                                 idx = 0;\r
115                                 bb = null;\r
116                         }\r
117                         \r
118                         public Field reset() {\r
119                                 idx = 0;\r
120                                 return this;\r
121                         }\r
122                         \r
123                         public String next() {\r
124                                 if(idx>=end)return null;\r
125                                 int start = idx;\r
126                                 byte c=0;\r
127                                 int endStr = -1;\r
128                                 while(idx<end && idx<buff.length && (c=buff[idx])!=delim && c!='\n') { // for DOS\r
129                                         if(c=='\r')endStr=idx;\r
130                                         ++idx;\r
131                                 }\r
132                                 \r
133                                 if(endStr<0) {\r
134                                         endStr=idx-start;\r
135                                 } else {\r
136                                         endStr=endStr-start;\r
137                                 }\r
138                                 ++idx;\r
139                                 return new String(buff,start,endStr);\r
140                         }\r
141 \r
142                         public String at(int fieldOffset) {\r
143                                 int start;\r
144                                 byte c=0;\r
145                                 for(int count = idx = start = 0; idx<end && idx<buff.length; ++idx) {\r
146                                         if((c=buff[idx])==delim || c=='\n') {\r
147                                                 if(count++ == fieldOffset) {\r
148                                                         break;\r
149                                                 }\r
150                                                 start = idx+1;\r
151                                         }\r
152                                 }\r
153                                 return new String(buff,start,(idx-start-(c=='\r'?1:0)));\r
154                         }\r
155                         \r
156                         public String atToEnd(int fieldOffset) {\r
157                                 int start;\r
158                                 byte c=0;\r
159                                 for(int count = idx = start = 0; idx<end && idx<buff.length; ++idx) {\r
160                                         if((c=buff[idx])==delim || c=='\n') {\r
161                                                 if(count++ == fieldOffset) {\r
162                                                         break;\r
163                                                 }\r
164                                                 start = idx+1;\r
165                                         }\r
166                                 }\r
167                                 \r
168                                 for(; idx<end && idx<buff.length && (c=buff[idx])!='\n'; ++idx) {\r
169                                         ++idx;\r
170                                 }\r
171                                 return new String(buff,start,(idx-start-((c=='\r' || idx>=end)?1:0)));\r
172                         }\r
173 \r
174                 }\r
175 \r
176                 public int pos() {\r
177                         return pos;\r
178                 }\r
179         }\r
180 \r
181         public File file() {\r
182                 return file;\r
183         }\r
184         \r
185 }\r