svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / 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 / AbstractPaginatedBand.java @ 43873
History | View | Annotate | Download (5.88 KB)
1 |
package org.gvsig.raster.lib.buffer.impl; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.nio.Buffer; |
5 |
|
6 |
import com.vividsolutions.jts.operation.IsSimpleOp; |
7 |
|
8 |
import org.slf4j.Logger; |
9 |
import org.slf4j.LoggerFactory; |
10 |
|
11 |
import org.gvsig.raster.lib.buffer.api.Band; |
12 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
13 |
import org.gvsig.raster.lib.buffer.api.BandNotification; |
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.BufferNotification; |
17 |
import org.gvsig.raster.lib.buffer.api.NoData; |
18 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException; |
19 |
import org.gvsig.tools.dispose.DisposeUtils; |
20 |
import org.gvsig.tools.exception.BaseException; |
21 |
|
22 |
/**
|
23 |
* @author fdiaz
|
24 |
*
|
25 |
*/
|
26 |
public abstract class AbstractPaginatedBand extends AbstractBand implements Band { |
27 |
|
28 |
private static final Logger logger = LoggerFactory.getLogger(AbstractPaginatedBand.class); |
29 |
protected Buffer data; |
30 |
protected int firstRowOfPage; |
31 |
protected int rowsPerPage; |
32 |
protected BandPageManager pageManager;
|
33 |
private boolean loaded; |
34 |
|
35 |
// protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
|
36 |
protected static final int MAX_PREFERED_SIZE = 2621440; // 2.5MB; |
37 |
|
38 |
/**
|
39 |
* @param rows
|
40 |
* @param columns
|
41 |
* @param noData
|
42 |
* @param pageManager
|
43 |
*/
|
44 |
public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) { |
45 |
this.rows = rows;
|
46 |
this.columns = columns;
|
47 |
this.loaded = false; |
48 |
calculateRowsPerPage(); |
49 |
|
50 |
if (noData == null) { |
51 |
this.noData = BufferLocator.getBufferManager().createNoData(null, null); |
52 |
} else {
|
53 |
this.noData = noData;
|
54 |
} |
55 |
this.pageManager = pageManager;
|
56 |
DisposeUtils.bind(pageManager); |
57 |
} |
58 |
|
59 |
private void calculateRowsPerPage() { |
60 |
rowsPerPage = MAX_PREFERED_SIZE / (this.columns * getDataSize());
|
61 |
if (rowsPerPage < 1) { |
62 |
rowsPerPage = 1;
|
63 |
} |
64 |
if (rowsPerPage > rows) {
|
65 |
rowsPerPage = rows; |
66 |
} |
67 |
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public void copyFrom(Band source) throws CopyFromBandException { |
72 |
doCopyFrom(source); |
73 |
notifyObservers(new DefaultBandNotification(BandNotification.COPY_FROM, new Object[] { source })); |
74 |
} |
75 |
|
76 |
protected void doCopyFrom(Band source) throws CopyFromBandException { |
77 |
if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows() |
78 |
|| this.getDataType() != source.getDataType()) {
|
79 |
throw new CopyFromBandException(source, this); |
80 |
} |
81 |
Object rowBuffer = this.createRowBuffer(); |
82 |
for (int row = 0; row < this.rows; row++) { |
83 |
source.fetchRow(row, rowBuffer); |
84 |
this.putRow(row, rowBuffer);
|
85 |
} |
86 |
} |
87 |
|
88 |
@Override
|
89 |
protected void doCopyFrom(Band source, int row, int column) throws CopyFromBandException { |
90 |
if (this.getColumns() <= row+source.getColumns() || this.getRows() <= column+source.getRows() |
91 |
|| this.getDataType()!=source.getDataType() ) {
|
92 |
throw new CopyFromBandException(source, this); |
93 |
} |
94 |
//FIXME:
|
95 |
Object rowBuffer = source.createRowBuffer();
|
96 |
for(int r=0; r<Math.min(this.getRows()-row, source.getRows()); r++){ |
97 |
source.fetchRow(r, rowBuffer); |
98 |
System.arraycopy(rowBuffer, 0, this.data, (r+row)*this.getColumns()+column, Math.min(this.getColumns()-column, source.getColumns())); |
99 |
this.putRow(r, rowBuffer);
|
100 |
} |
101 |
} |
102 |
|
103 |
protected void loadPage(int row) { |
104 |
if (loaded && row >= firstRowOfPage && row < firstRowOfPage + rowsPerPage) {
|
105 |
return;
|
106 |
} |
107 |
loaded = false;
|
108 |
|
109 |
if(this.pageManager.isSupportedSave()){ |
110 |
saveCurrentPage(); |
111 |
} |
112 |
int currentPage = row / rowsPerPage; // Divisi?n entera |
113 |
firstRowOfPage = currentPage * rowsPerPage; |
114 |
try {
|
115 |
int rowsInPage = rowsPerPage;
|
116 |
if (firstRowOfPage + rowsPerPage > this.rows) { |
117 |
rowsInPage = this.rows - firstRowOfPage;
|
118 |
} |
119 |
this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType()); |
120 |
loaded = true;
|
121 |
this.notifyObservers(new DefaultBufferNotification(BandNotification.LOADED_PAGE, new Object[] { this })); |
122 |
} catch (IOException e) { |
123 |
throw new RuntimeException("Can't load current page", e); |
124 |
} |
125 |
} |
126 |
|
127 |
protected void saveCurrentPage() { |
128 |
try {
|
129 |
int rowsInPage = rowsPerPage;
|
130 |
if (firstRowOfPage + rowsPerPage > this.rows) { |
131 |
rowsInPage = this.rows - firstRowOfPage;
|
132 |
} |
133 |
this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType()); |
134 |
} catch (UnsupportedOperationException e) { |
135 |
// Do nothing, operation not supported
|
136 |
} catch (IOException e) { |
137 |
throw new RuntimeException("Can't save current page", e); |
138 |
} |
139 |
} |
140 |
|
141 |
protected abstract int getDataSize(); |
142 |
|
143 |
@Override
|
144 |
public boolean isReadOnly() { |
145 |
if (this.pageManager == null) { |
146 |
return false; |
147 |
} |
148 |
return !this.pageManager.isSupportedSave(); |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public boolean isPaginated() { |
153 |
return true; |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public BandInfo getBandInfo() {
|
158 |
return this.pageManager == null ? null : this.pageManager.getBandInfo(); |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Calculate buffer size than must be allocated.
|
163 |
*
|
164 |
* @param rows
|
165 |
* @param columns
|
166 |
* @return
|
167 |
*/
|
168 |
protected int calculateBufferSizeToAllocate(int rows, int columns) { |
169 |
return rowsPerPage * columns;
|
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void doDispose() throws BaseException { |
174 |
super.doDispose();
|
175 |
data = null;
|
176 |
DisposeUtils.dispose(pageManager); |
177 |
pageManager = null;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
protected void finalize() throws Throwable { |
182 |
super.finalize();
|
183 |
} |
184 |
|
185 |
|
186 |
} |