+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package com.att.authz.local;\r
-\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.io.RandomAccessFile;\r
-import java.nio.ByteBuffer;\r
-import java.nio.IntBuffer;\r
-import java.nio.MappedByteBuffer;\r
-import java.nio.channels.FileChannel;\r
-import java.nio.channels.FileChannel.MapMode;\r
-\r
-public class DataFile {\r
- private RandomAccessFile rafile;\r
- private FileChannel channel;\r
- public MappedByteBuffer mapBuff;\r
- private final File file;\r
- private final String access;\r
- \r
- public DataFile(File file, String access) {\r
- this.file = file;\r
- this.access = access;\r
- }\r
- public void open() throws IOException {\r
- if(!file.exists()) throw new FileNotFoundException();\r
- rafile = new RandomAccessFile(file,access);\r
- channel = rafile.getChannel();\r
- mapBuff = channel.map("r".equals(access)?MapMode.READ_ONLY:MapMode.READ_WRITE,0,channel.size());\r
- }\r
- public void close() throws IOException {\r
- if(channel!=null){channel.close();}\r
- if(rafile!=null) {rafile.close();}\r
- mapBuff = null;\r
- }\r
-\r
- public long size() throws IOException {\r
- return channel.size();\r
- }\r
-\r
- private synchronized int load(Token t) {\r
- int len = Math.min(mapBuff.limit()-t.next,t.buff.length);\r
- if(len>0) {\r
- mapBuff.position(t.next);\r
- mapBuff.get(t.buff,0,len);\r
- }\r
- return len<0?0:len;\r
- }\r
- \r
- public class Token {\r
- private byte[] buff;\r
- int pos, next, end;\r
- \r
- public Token(int size) {\r
- buff = new byte[size];\r
- pos = next = end = 0;\r
- }\r
- \r
- public boolean pos(int to) {\r
- pos = next = to;\r
- return (end=load(this))>0;\r
- }\r
- \r
- public boolean nextLine() {\r
- end = load(this);\r
- pos = next;\r
- for(int i=0;i<end;++i) {\r
- if(buff[i]=='\n') {\r
- end = i;\r
- next += i+1;\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
- \r
- public IntBuffer getIntBuffer() {\r
- return ByteBuffer.wrap(buff).asIntBuffer();\r
- }\r
-\r
-\r
-\r
- public String toString() {\r
- return new String(buff,0,end);\r
- }\r
- public class Field {\r
- char delim;\r
- int idx;\r
- ByteBuffer bb;\r
-\r
- public Field(char delimiter) {\r
- delim = delimiter;\r
- idx = 0;\r
- bb = null;\r
- }\r
- \r
- public Field reset() {\r
- idx = 0;\r
- return this;\r
- }\r
- \r
- public String next() {\r
- if(idx>=end)return null;\r
- int start = idx;\r
- byte c=0;\r
- int endStr = -1;\r
- while(idx<end && idx<buff.length && (c=buff[idx])!=delim && c!='\n') { // for DOS\r
- if(c=='\r')endStr=idx;\r
- ++idx;\r
- }\r
- \r
- if(endStr<0) {\r
- endStr=idx-start;\r
- } else {\r
- endStr=endStr-start;\r
- }\r
- ++idx;\r
- return new String(buff,start,endStr);\r
- }\r
-\r
- public String at(int fieldOffset) {\r
- int start;\r
- byte c=0;\r
- for(int count = idx = start = 0; idx<end && idx<buff.length; ++idx) {\r
- if((c=buff[idx])==delim || c=='\n') {\r
- if(count++ == fieldOffset) {\r
- break;\r
- }\r
- start = idx+1;\r
- }\r
- }\r
- return new String(buff,start,(idx-start-(c=='\r'?1:0)));\r
- }\r
- \r
- public String atToEnd(int fieldOffset) {\r
- int start;\r
- byte c=0;\r
- for(int count = idx = start = 0; idx<end && idx<buff.length; ++idx) {\r
- if((c=buff[idx])==delim || c=='\n') {\r
- if(count++ == fieldOffset) {\r
- break;\r
- }\r
- start = idx+1;\r
- }\r
- }\r
- \r
- for(; idx<end && idx<buff.length && (c=buff[idx])!='\n'; ++idx) {\r
- ++idx;\r
- }\r
- return new String(buff,start,(idx-start-((c=='\r' || idx>=end)?1:0)));\r
- }\r
-\r
- }\r
-\r
- public int pos() {\r
- return pos;\r
- }\r
- }\r
-\r
- public File file() {\r
- return file;\r
- }\r
- \r
-}\r