2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.local;
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;
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;
41 public DataFile(File file, String access) {
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());
51 public boolean isOpened() {
54 public void close() throws IOException {
64 public long size() throws IOException {
65 return channel==null?0:channel.size();
68 private synchronized int load(Token t) {
69 int len = Math.min(mapBuff.limit()-t.next,t.buff.length);
71 mapBuff.position(t.next);
72 mapBuff.get(t.buff,0,len);
81 public Token(int size) {
82 buff = new byte[size];
86 public boolean pos(int to) {
88 return (end=load(this))>0;
91 public boolean nextLine() {
94 for (int i=0;i<end;++i) {
104 public IntBuffer getIntBuffer() {
105 return ByteBuffer.wrap(buff).asIntBuffer();
108 public String toString() {
109 return new String(buff,0,end);
117 public Field(char delimiter) {
123 public Field reset() {
128 public String next() {
129 if (idx>=end)return null;
133 while (idx<end && idx<buff.length && (c=buff[idx])!=delim && c!='\n') { // for DOS
134 if (c=='\r')endStr=idx;
144 return new String(buff,start,endStr);
147 public String at(int fieldOffset) {
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) {
158 return new String(buff,start,(idx-start-(c=='\r'?1:0)));
161 public String atToEnd(int fieldOffset) {
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) {
173 for (; idx<end && idx<buff.length && (c=buff[idx])!='\n'; ++idx) {
176 return new String(buff,start,(idx-start-((c=='\r' || idx>=end)?1:0)));