svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / SimpleBandPageManager.java @ 44831
History | View | Annotate | Download (4.98 KB)
1 | 44831 | jjdelcerro | package org.gvsig.raster.lib.buffer.impl; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | import java.io.RandomAccessFile; |
||
6 | import java.nio.Buffer; |
||
7 | import java.nio.ByteBuffer; |
||
8 | import java.nio.DoubleBuffer; |
||
9 | import java.nio.FloatBuffer; |
||
10 | import java.nio.IntBuffer; |
||
11 | import java.nio.ShortBuffer; |
||
12 | |||
13 | import org.gvsig.raster.lib.buffer.api.BandInfo; |
||
14 | import org.gvsig.raster.lib.buffer.api.BandPageManager; |
||
15 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
16 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
17 | import org.gvsig.tools.dispose.impl.AbstractDisposable; |
||
18 | import org.gvsig.tools.exception.BaseException; |
||
19 | |||
20 | |||
21 | /**
|
||
22 | * @author fdiaz
|
||
23 | *
|
||
24 | */
|
||
25 | public class SimpleBandPageManager extends AbstractDisposable implements BandPageManager { |
||
26 | |||
27 | private RandomAccessFile raf; |
||
28 | private int rows; |
||
29 | private int columns; |
||
30 | private int dataType; |
||
31 | private File file; |
||
32 | |||
33 | /**
|
||
34 | * @param file
|
||
35 | * @param rows
|
||
36 | * @param columns
|
||
37 | * @param dataType
|
||
38 | * @throws IOException
|
||
39 | */
|
||
40 | public SimpleBandPageManager(File file, int rows, int columns, int dataType) throws IOException { |
||
41 | this.rows = rows;
|
||
42 | this.columns = columns;
|
||
43 | this.dataType = dataType;
|
||
44 | this.file = file;
|
||
45 | if(this.file==null){ |
||
46 | this.file = File.createTempFile("bandPageManager", "tmp"); |
||
47 | } |
||
48 | |||
49 | this.raf = new RandomAccessFile(this.file, "rw"); |
||
50 | BufferManager bufferManager = BufferLocator.getBufferManager(); |
||
51 | raf.setLength(rows*columns*bufferManager.getDataTypeSize(dataType)); |
||
52 | |||
53 | } |
||
54 | |||
55 | public void load(Buffer buffer, int row, int rows, int dataType) throws IOException { |
||
56 | |||
57 | int pos = row*this.columns; |
||
58 | raf.seek(pos); |
||
59 | BufferManager bufferManager = BufferLocator.getBufferManager(); |
||
60 | int dataTypeSize = bufferManager.getDataTypeSize(dataType);
|
||
61 | byte[] byteArray = new byte[buffer.capacity() * dataTypeSize]; |
||
62 | raf.read(byteArray); |
||
63 | buffer.rewind(); |
||
64 | |||
65 | switch (dataType) {
|
||
66 | case BufferManager.TYPE_BYTE:
|
||
67 | //FIXME: Elegir cual de las dos siguientes maneras de hacerlo es la id?nea
|
||
68 | |||
69 | ((ByteBuffer) buffer).put(byteArray, 0, byteArray.length); |
||
70 | // System.arraycopy(byteArray, 0, ((ByteBuffer) buffer).array(), 0, byteArray.length);
|
||
71 | break;
|
||
72 | case BufferManager.TYPE_SHORT:
|
||
73 | case BufferManager.TYPE_USHORT:
|
||
74 | for( int cell =0 ; cell<byteArray.length ; cell++ ) { |
||
75 | ((ShortBuffer) buffer).put((int)(cell/dataTypeSize), byteArray[cell]); |
||
76 | } |
||
77 | break;
|
||
78 | case BufferManager.TYPE_INT:
|
||
79 | for( int cell =0 ; cell<byteArray.length ; cell++ ) { |
||
80 | ((IntBuffer) buffer).put((int)(cell/dataTypeSize), byteArray[cell]); |
||
81 | } |
||
82 | break;
|
||
83 | case BufferManager.TYPE_FLOAT:
|
||
84 | for( int cell =0 ; cell<byteArray.length ; cell++ ) { |
||
85 | ((FloatBuffer) buffer).put((int)(cell/dataTypeSize), byteArray[cell]); |
||
86 | } |
||
87 | break;
|
||
88 | |||
89 | case BufferManager.TYPE_DOUBLE:
|
||
90 | for( int cell =0 ; cell<byteArray.length ; cell++ ) { |
||
91 | ((DoubleBuffer) buffer).put((int)(cell/dataTypeSize), byteArray[cell]); |
||
92 | } |
||
93 | break;
|
||
94 | |||
95 | default:
|
||
96 | throw new IllegalArgumentException("Data Type " + dataType + " not supported."); |
||
97 | } |
||
98 | |||
99 | |||
100 | |||
101 | |||
102 | } |
||
103 | |||
104 | public void save(Buffer buffer, int row, int rows, int dataType) throws IOException { |
||
105 | |||
106 | int pos = row*this.columns; |
||
107 | raf.seek(pos); |
||
108 | |||
109 | BufferManager bufferManager = BufferLocator.getBufferManager(); |
||
110 | ByteBuffer byteBuffer = ByteBuffer.allocate(buffer.capacity() * bufferManager.getDataTypeSize(dataType)); |
||
111 | switch (dataType) {
|
||
112 | case BufferManager.TYPE_BYTE:
|
||
113 | byteBuffer.put(((ByteBuffer)buffer).array());
|
||
114 | break;
|
||
115 | case BufferManager.TYPE_SHORT:
|
||
116 | case BufferManager.TYPE_USHORT:
|
||
117 | ShortBuffer byteAsShortBuffer = byteBuffer.asShortBuffer();
|
||
118 | byteAsShortBuffer.put((short[]) buffer.array()); |
||
119 | break;
|
||
120 | case BufferManager.TYPE_INT:
|
||
121 | IntBuffer byteAsIntBuffer = byteBuffer.asIntBuffer();
|
||
122 | byteAsIntBuffer.put((int[]) buffer.array()); |
||
123 | break;
|
||
124 | case BufferManager.TYPE_FLOAT:
|
||
125 | FloatBuffer byteAsFloatBuffer = byteBuffer.asFloatBuffer();
|
||
126 | byteAsFloatBuffer.put((float[]) buffer.array()); |
||
127 | break;
|
||
128 | case BufferManager.TYPE_DOUBLE:
|
||
129 | DoubleBuffer byteAsDoubleBuffer = byteBuffer.asDoubleBuffer();
|
||
130 | byteAsDoubleBuffer.put((double[]) buffer.array()); |
||
131 | break;
|
||
132 | default:
|
||
133 | throw new IllegalArgumentException("Data Type " + dataType + " not supported."); |
||
134 | } |
||
135 | raf.write(byteBuffer.array()); |
||
136 | |||
137 | } |
||
138 | |||
139 | @Override
|
||
140 | public boolean isSupportedSave() { |
||
141 | return true; |
||
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public BandInfo getBandInfo() {
|
||
146 | return null; |
||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | protected void doDispose() throws BaseException { |
||
151 | this.raf = null; |
||
152 | this.file = null; |
||
153 | } |
||
154 | } |