Statistics
| Revision:

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 @ 5515

History | View | Annotate | Download (3.67 KB)

1
package org.gvsig.raster.lib.buffer.impl;
2

    
3
import java.io.IOException;
4
import java.nio.ByteBuffer;
5

    
6
import org.gvsig.raster.lib.buffer.api.Band;
7
import org.gvsig.raster.lib.buffer.api.BandPageManager;
8
import org.gvsig.raster.lib.buffer.api.BufferLocator;
9
import org.gvsig.raster.lib.buffer.api.NoData;
10
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException;
11

    
12
/**
13
 * @author fdiaz
14
 *
15
 */
16
public abstract class AbstractPaginatedBand extends AbstractBand {
17

    
18
    protected ByteBuffer data;
19
    protected int firstRowOfPage;
20
    protected int rowsPerPage;
21
    protected BandPageManager pageManager;
22

    
23
    protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
24

    
25
    /**
26
     * @param rows
27
     * @param columns
28
     * @param noData
29
     * @param rowsPerPage
30
     * @param pageManager
31
     */
32
    public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) {
33
        this.rows = rows;
34
        this.columns = columns;
35
        calculateRowsPerPage();
36
        data = ByteBuffer.allocate(rowsPerPage * columns * getDataSize());
37
        if (noData == null) {
38
            this.noData = BufferLocator.getBufferManager().createNoData(null, null);
39
        } else {
40
            this.noData = noData;
41
        }
42
        this.pageManager = pageManager;
43
    }
44

    
45
    private void calculateRowsPerPage(){
46
        rowsPerPage = MAX_PREFERED_SIZE/(this.columns*getDataSize());
47
        if(rowsPerPage<1){
48
            rowsPerPage = 1;
49
        }
50
    }
51

    
52
    /**
53
     * @param value
54
     * @return
55
     */
56
    protected Object nullValueToNoData(Object value) {
57
        if (value == null) {
58
            if (getNoData().isDefined()) {
59
                value = getNoData().getValue();
60
            } else {
61
                // Do nothing, no data value is undefined
62
                return null;
63
            }
64
        }
65
        return value;
66
    }
67

    
68
    @Override
69
    public void copyFrom(Band source) throws CopyFromBandException {
70
        if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows()
71
            || this.getDataType()!=source.getDataType() ) {
72
            throw new CopyFromBandException(source, this);
73
        }
74
        Object rowBuffer = this.createRowBuffer();
75
        for(int row = 0; row<=this.rows; row++){
76
            source.fetchRow(row, rowBuffer);
77
            this.putRow(row, rowBuffer);
78
        }
79
    }
80

    
81
    protected void loadPage(int row) {
82
        if(row>=firstRowOfPage && row<firstRowOfPage+rowsPerPage){
83
            return;
84
        }
85
        saveCurrentPage();
86
        firstRowOfPage = row / rowsPerPage; //Divisi?n entera
87
        try {
88
            int rowsInPage = rowsPerPage;
89
            if(firstRowOfPage + rowsPerPage > this.rows){
90
                rowsInPage = this.rows - firstRowOfPage;
91
            }
92
            this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType());
93
        } catch (IOException e) {
94
            throw new RuntimeException("Can't save 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
}