svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / virtualrows / RandomAccessFileIndex.java @ 47655
History | View | Annotate | Download (5.03 KB)
1 |
package org.gvsig.fmap.dal.store.simplereader.virtualrows; |
---|---|
2 |
|
3 |
import java.io.Closeable; |
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.io.RandomAccessFile; |
7 |
import java.nio.LongBuffer; |
8 |
import java.nio.MappedByteBuffer; |
9 |
import java.nio.channels.FileChannel; |
10 |
import java.util.AbstractList; |
11 |
import org.apache.commons.io.IOUtils; |
12 |
import org.gvsig.tools.util.GetItemWithSize64; |
13 |
import org.gvsig.tools.util.SetItem; |
14 |
|
15 |
/**
|
16 |
*
|
17 |
* @author gvSIG Team
|
18 |
*/
|
19 |
public class RandomAccessFileIndex |
20 |
extends AbstractList<Long> |
21 |
implements Closeable, GetItemWithSize64<Long>, SetItem<Integer, Long> |
22 |
{ |
23 |
|
24 |
private static final int SIZE_OF_LONG = 8; |
25 |
|
26 |
private static final int HEADER_SIZE = 20; |
27 |
|
28 |
private RandomAccessFile raf; |
29 |
private MappedByteBuffer fileByteBuffer; |
30 |
private LongBuffer buffer; |
31 |
private long sz; |
32 |
|
33 |
public RandomAccessFileIndex() {
|
34 |
|
35 |
} |
36 |
|
37 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
38 |
public RandomAccessFileIndex(File f) throws IOException { |
39 |
this.open(f);
|
40 |
} |
41 |
|
42 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
43 |
public RandomAccessFileIndex(RandomAccessFile raf) throws IOException { |
44 |
this.open(raf);
|
45 |
} |
46 |
|
47 |
public void open(File f) throws IOException { |
48 |
RandomAccessFile theRaf = new RandomAccessFile(f,"r"); |
49 |
this.open(theRaf);
|
50 |
} |
51 |
|
52 |
public void open(RandomAccessFile raf) throws IOException { |
53 |
this.raf = raf;
|
54 |
this.fileByteBuffer = this.raf.getChannel().map( |
55 |
FileChannel.MapMode.READ_ONLY,
|
56 |
0,
|
57 |
this.raf.length()
|
58 |
); |
59 |
this.buffer = this.fileByteBuffer.asLongBuffer(); |
60 |
this.sz = buffer.limit()-HEADER_SIZE;
|
61 |
} |
62 |
|
63 |
public void create(File f, long sz) throws IOException { |
64 |
RandomAccessFile theRaf = new RandomAccessFile(f,"rw"); |
65 |
this.create(theRaf,sz);
|
66 |
} |
67 |
|
68 |
public void create(RandomAccessFile raf, long numElements) throws IOException { |
69 |
this.raf = raf;
|
70 |
this.raf.setLength((numElements+HEADER_SIZE)*SIZE_OF_LONG);
|
71 |
this.fileByteBuffer = this.raf.getChannel().map( |
72 |
FileChannel.MapMode.READ_WRITE,
|
73 |
0,
|
74 |
this.raf.length()
|
75 |
); |
76 |
this.buffer = this.fileByteBuffer.asLongBuffer(); |
77 |
this.sz = buffer.limit()-HEADER_SIZE;
|
78 |
} |
79 |
|
80 |
public void setNumElements(long numElements) throws IOException { |
81 |
long size = ((numElements+HEADER_SIZE)*SIZE_OF_LONG);
|
82 |
this.fileByteBuffer.force();
|
83 |
this.raf.setLength(size);
|
84 |
this.sz=numElements;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void close() throws IOException { |
89 |
this.fileByteBuffer.force();
|
90 |
IOUtils.closeQuietly(this.raf);
|
91 |
this.buffer = null; |
92 |
this.fileByteBuffer = null; |
93 |
this.sz = -1; |
94 |
} |
95 |
|
96 |
public boolean isOpen() { |
97 |
return this.buffer!=null; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public Long get(int position) { |
102 |
position = checkIndex(position); |
103 |
return this.buffer.get(position+HEADER_SIZE); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public int size() { |
108 |
return (int) this.sz; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public long size64() { |
113 |
return this.sz; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public Long get64(long position) { |
118 |
position = checkIndex(position); |
119 |
return this.buffer.get((int)position+HEADER_SIZE); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public void set(Integer position, Long value) { |
124 |
position = checkIndex(position); |
125 |
this.buffer.put(position+HEADER_SIZE, value);
|
126 |
} |
127 |
|
128 |
public void set(int position, long value) { |
129 |
position = checkIndex(position); |
130 |
this.buffer.put(position+HEADER_SIZE, value);
|
131 |
} |
132 |
|
133 |
public void setHeader(int index, long value) { |
134 |
index = checkHeaderIndex(index); |
135 |
this.buffer.put(index, value);
|
136 |
} |
137 |
|
138 |
public long getHeader(int index) { |
139 |
index = checkHeaderIndex(index); |
140 |
return this.buffer.get(index); |
141 |
} |
142 |
|
143 |
private int checkIndex(long index) { |
144 |
if (this.buffer == null) { |
145 |
throw new IllegalStateException("Index not open"); |
146 |
} |
147 |
if( index < 0 ) { |
148 |
index = ((int)this.sz) + index; |
149 |
} |
150 |
if( index<0 || index>=this.sz) { |
151 |
throw new IllegalArgumentException("Index out of range ("+index+")"); |
152 |
} |
153 |
return (int) index; |
154 |
} |
155 |
|
156 |
private int checkHeaderIndex(int index) { |
157 |
if (this.buffer == null) { |
158 |
throw new IllegalStateException("Index not open"); |
159 |
} |
160 |
if( index < 0 ) { |
161 |
index = HEADER_SIZE + index; |
162 |
} |
163 |
if( index<0 || index>=HEADER_SIZE) { |
164 |
throw new IllegalArgumentException("Index out of range ("+index+")"); |
165 |
} |
166 |
return index;
|
167 |
} |
168 |
|
169 |
// public void add(int initpos, Iterator<Long> values) {
|
170 |
// if (this.buffer == null) {
|
171 |
// throw new IllegalStateException("Index not open");
|
172 |
// }
|
173 |
// int position = initpos;
|
174 |
// while( values.hasNext() ) {
|
175 |
// Long value = values.next();
|
176 |
// this.buffer.put(position++, value);
|
177 |
// }
|
178 |
// }
|
179 |
|
180 |
} |