1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.authz.local;
\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
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
42 public DataFile(File file, String access) {
\r
44 this.access = access;
\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
52 public void close() throws IOException {
\r
53 if(channel!=null){channel.close();}
\r
54 if(rafile!=null) {rafile.close();}
\r
58 public long size() throws IOException {
\r
59 return channel.size();
\r
62 private synchronized int load(Token t) {
\r
63 int len = Math.min(mapBuff.limit()-t.next,t.buff.length);
\r
65 mapBuff.position(t.next);
\r
66 mapBuff.get(t.buff,0,len);
\r
71 public class Token {
\r
72 private byte[] buff;
\r
75 public Token(int size) {
\r
76 buff = new byte[size];
\r
77 pos = next = end = 0;
\r
80 public boolean pos(int to) {
\r
82 return (end=load(this))>0;
\r
85 public boolean nextLine() {
\r
88 for(int i=0;i<end;++i) {
\r
98 public IntBuffer getIntBuffer() {
\r
99 return ByteBuffer.wrap(buff).asIntBuffer();
\r
104 public String toString() {
\r
105 return new String(buff,0,end);
\r
107 public class Field {
\r
112 public Field(char delimiter) {
\r
118 public Field reset() {
\r
123 public String next() {
\r
124 if(idx>=end)return null;
\r
128 while(idx<end && idx<buff.length && (c=buff[idx])!=delim && c!='\n') { // for DOS
\r
129 if(c=='\r')endStr=idx;
\r
136 endStr=endStr-start;
\r
139 return new String(buff,start,endStr);
\r
142 public String at(int fieldOffset) {
\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
153 return new String(buff,start,(idx-start-(c=='\r'?1:0)));
\r
156 public String atToEnd(int fieldOffset) {
\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
168 for(; idx<end && idx<buff.length && (c=buff[idx])!='\n'; ++idx) {
\r
171 return new String(buff,start,(idx-start-((c=='\r' || idx>=end)?1:0)));
\r
181 public File file() {
\r