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