gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / AbstractPaginatedBand.java @ 6487
History | View | Annotate | Download (4.79 KB)
1 |
package org.gvsig.raster.lib.buffer.impl; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.nio.Buffer; |
5 |
|
6 |
import org.slf4j.Logger; |
7 |
import org.slf4j.LoggerFactory; |
8 |
|
9 |
import org.gvsig.raster.lib.buffer.api.Band; |
10 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
11 |
import org.gvsig.raster.lib.buffer.api.BandNotification; |
12 |
import org.gvsig.raster.lib.buffer.api.BandPageManager; |
13 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
14 |
import org.gvsig.raster.lib.buffer.api.NoData; |
15 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException; |
16 |
|
17 |
/**
|
18 |
* @author fdiaz
|
19 |
*
|
20 |
*/
|
21 |
public abstract class AbstractPaginatedBand extends AbstractBand implements Band { |
22 |
|
23 |
private static final Logger logger = LoggerFactory.getLogger(AbstractPaginatedBand.class); |
24 |
protected Buffer data; |
25 |
protected int firstRowOfPage; |
26 |
protected int rowsPerPage; |
27 |
protected BandPageManager pageManager;
|
28 |
private boolean loaded; |
29 |
|
30 |
protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB; |
31 |
|
32 |
/**
|
33 |
* @param rows
|
34 |
* @param columns
|
35 |
* @param noData
|
36 |
* @param pageManager
|
37 |
*/
|
38 |
public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) { |
39 |
this.rows = rows;
|
40 |
this.columns = columns;
|
41 |
this.loaded = false; |
42 |
calculateRowsPerPage(); |
43 |
|
44 |
if (noData == null) { |
45 |
this.noData = BufferLocator.getBufferManager().createNoData(null, null); |
46 |
} else {
|
47 |
this.noData = noData;
|
48 |
} |
49 |
this.pageManager = pageManager;
|
50 |
} |
51 |
|
52 |
private void calculateRowsPerPage() { |
53 |
rowsPerPage = MAX_PREFERED_SIZE / (this.columns * getDataSize());
|
54 |
if (rowsPerPage < 1) { |
55 |
rowsPerPage = 1;
|
56 |
} |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public void copyFrom(Band source) throws CopyFromBandException { |
61 |
doCopyFrom(source); |
62 |
notifyObservers(new DefaultBandNotification(BandNotification.COPY_FROM, new Object[] { source })); |
63 |
} |
64 |
|
65 |
protected void doCopyFrom(Band source) throws CopyFromBandException { |
66 |
if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows() |
67 |
|| this.getDataType() != source.getDataType()) {
|
68 |
throw new CopyFromBandException(source, this); |
69 |
} |
70 |
Object rowBuffer = this.createRowBuffer(); |
71 |
for (int row = 0; row <= this.rows; row++) { |
72 |
source.fetchRow(row, rowBuffer); |
73 |
this.putRow(row, rowBuffer);
|
74 |
} |
75 |
} |
76 |
|
77 |
protected void loadPage(int row) { |
78 |
if (loaded && row >= firstRowOfPage && row < firstRowOfPage + rowsPerPage) {
|
79 |
return;
|
80 |
} |
81 |
loaded = false;
|
82 |
|
83 |
saveCurrentPage(); |
84 |
int currentPage = row / rowsPerPage; // Divisi?n entera |
85 |
firstRowOfPage = currentPage * rowsPerPage; |
86 |
try {
|
87 |
int rowsInPage = rowsPerPage;
|
88 |
if (firstRowOfPage + rowsPerPage > this.rows) { |
89 |
rowsInPage = this.rows - firstRowOfPage;
|
90 |
} |
91 |
this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType()); |
92 |
loaded = true;
|
93 |
} catch (IOException e) { |
94 |
throw new RuntimeException("Can't load current page", e); |
95 |
} |
96 |
} |
97 |
|
98 |
protected void saveCurrentPage() { |
99 |
try {
|
100 |
int rowsInPage = rowsPerPage;
|
101 |
if (firstRowOfPage + rowsPerPage > this.rows) { |
102 |
rowsInPage = this.rows - firstRowOfPage;
|
103 |
} |
104 |
this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType()); |
105 |
} catch (UnsupportedOperationException e) { |
106 |
// Do nothing, operation not supported
|
107 |
} catch (IOException e) { |
108 |
throw new RuntimeException("Can't save current page", e); |
109 |
} |
110 |
} |
111 |
|
112 |
protected abstract int getDataSize(); |
113 |
|
114 |
@Override
|
115 |
public boolean isReadOnly() { |
116 |
if (this.pageManager == null) { |
117 |
return false; |
118 |
} |
119 |
return !this.pageManager.isSupportedSave(); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public boolean isPaginated() { |
124 |
return true; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public BandInfo getBandInfo() {
|
129 |
return this.pageManager == null ? null : this.pageManager.getBandInfo(); |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Calculate buffer size than must be allocated.
|
134 |
*
|
135 |
* @param rows
|
136 |
* @param columns
|
137 |
* @return
|
138 |
*/
|
139 |
protected int calculateBufferSizeToAllocate(int rows, int columns) { |
140 |
// If the number of requested cells is less than the default number of
|
141 |
// cells, it is unnecessary to reserve the maximum possible size.
|
142 |
|
143 |
if (rows < rowsPerPage) {
|
144 |
logger.info("ALLOCATE rows = " + rows + " columns = " + columns + " bytes = " + (rows * columns)); |
145 |
return rows * columns;
|
146 |
} |
147 |
logger.info("ALLOCATE rowsPerPage = " + rowsPerPage + " columns = " + columns + " bytes = " |
148 |
+ (rowsPerPage * columns)); |
149 |
return rowsPerPage * columns;
|
150 |
} |
151 |
|
152 |
} |