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

History | View | Annotate | Download (4.02 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.BandNotification;
8
import org.gvsig.raster.lib.buffer.api.BandPageManager;
9
import org.gvsig.raster.lib.buffer.api.BufferLocator;
10
import org.gvsig.raster.lib.buffer.api.NoData;
11
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException;
12

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

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

    
24

    
25
    protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
26

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

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

    
54
    /**
55
     * @param value
56
     * @return
57
     */
58
    protected Object nullValueToNoData(Object value) {
59
        if (value == null) {
60
            if (getNoData().isDefined()) {
61
                value = getNoData().getValue();
62
            } else {
63
                // Do nothing, no data value is undefined
64
                return null;
65
            }
66
        }
67
        return value;
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
    protected void loadPage(int row) {
89
        if(!(row>=firstRowOfPage && row<firstRowOfPage+rowsPerPage)){
90
            return;
91
        }
92
        saveCurrentPage();
93
        firstRowOfPage = row / rowsPerPage; //Divisi?n entera
94
        try {
95
            int rowsInPage = rowsPerPage;
96
            if(firstRowOfPage + rowsPerPage > this.rows){
97
                rowsInPage = this.rows - firstRowOfPage;
98
            }
99
            this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType());
100
        } catch (IOException e) {
101
            throw new RuntimeException("Can't save current page", e);
102
        }
103
    }
104

    
105
    protected void saveCurrentPage() {
106
        try {
107
            int rowsInPage = rowsPerPage;
108
            if(firstRowOfPage + rowsPerPage > this.rows){
109
                rowsInPage = this.rows - firstRowOfPage;
110
            }
111
            this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType());
112
        } catch (UnsupportedOperationException e) {
113
            // Do nothing, operation not supported
114
        } catch (IOException e) {
115
            throw new RuntimeException("Can't save current page", e);
116
        }
117
    }
118

    
119
    protected abstract int getDataSize();
120

    
121
    @Override
122
    public boolean isReadOnly() {
123
        if(this.pageManager==null){
124
            return false;
125
        }
126
        return !this.pageManager.isSupportedSave();
127
    }
128

    
129

    
130
    @Override
131
    public boolean isPaginated() {
132
        return true;
133
    }
134

    
135
}