1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.authz.local;
\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
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
43 public DataFile(File file, String access) {
\r
45 this.access = access;
\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
53 public void close() throws IOException {
\r
54 if(channel!=null){channel.close();}
\r
55 if(rafile!=null) {rafile.close();}
\r
59 public long size() throws IOException {
\r
60 return channel.size();
\r
63 private synchronized int load(Token t) {
\r
64 int len = Math.min(mapBuff.limit()-t.next,t.buff.length);
\r
66 mapBuff.position(t.next);
\r
67 mapBuff.get(t.buff,0,len);
\r
72 public class Token {
\r
73 private byte[] buff;
\r
76 public Token(int size) {
\r
77 buff = new byte[size];
\r
78 pos = next = end = 0;
\r
81 public boolean pos(int to) {
\r
83 return (end=load(this))>0;
\r
86 public boolean nextLine() {
\r
89 for(int i=0;i<end;++i) {
\r
99 public IntBuffer getIntBuffer() {
\r
100 return ByteBuffer.wrap(buff).asIntBuffer();
\r
105 public String toString() {
\r
106 return new String(buff,0,end);
\r
108 public class Field {
\r
113 public Field(char delimiter) {
\r
119 public Field reset() {
\r
124 public String next() {
\r
125 if(idx>=end)return null;
\r
129 while(idx<end && idx<buff.length && (c=buff[idx])!=delim && c!='\n') { // for DOS
\r
130 if(c=='\r')endStr=idx;
\r
137 endStr=endStr-start;
\r
140 return new String(buff,start,endStr);
\r
143 public String at(int fieldOffset) {
\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
154 return new String(buff,start,(idx-start-(c=='\r'?1:0)));
\r
157 public String atToEnd(int fieldOffset) {
\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
169 for(; idx<end && idx<buff.length && (c=buff[idx])!='\n'; ++idx) {
\r
172 return new String(buff,start,(idx-start-((c=='\r' || idx>=end)?1:0)));
\r
182 public File file() {
\r