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