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

History | View | Annotate | Download (3.91 KB)

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

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

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

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

    
20
    protected Buffer data;
21
    protected int firstRowOfPage;
22
    protected int rowsPerPage;
23
    protected BandPageManager pageManager;
24
    private boolean loaded;
25

    
26

    
27
    protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
28

    
29
    /**
30
     * @param rows
31
     * @param columns
32
     * @param noData
33
     * @param pageManager
34
     */
35
    public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) {
36
        this.rows = rows;
37
        this.columns = columns;
38
        this.loaded=false;
39
        calculateRowsPerPage();
40

    
41
        if (noData == null) {
42
            this.noData = BufferLocator.getBufferManager().createNoData(null, null);
43
        } else {
44
            this.noData = noData;
45
        }
46
        this.pageManager = pageManager;
47
    }
48

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

    
56

    
57
    @Override
58
    public void copyFrom(Band source) throws CopyFromBandException {
59
        doCopyFrom(source);
60
        notifyObservers(new DefaultBandNotification(BandNotification.COPY_FROM, new Object[]{source}));
61
    }
62

    
63
    protected void doCopyFrom(Band source) throws CopyFromBandException {
64
        if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows()
65
            || this.getDataType()!=source.getDataType() ) {
66
            throw new CopyFromBandException(source, this);
67
        }
68
        Object rowBuffer = this.createRowBuffer();
69
        for(int row = 0; row<=this.rows; row++){
70
            source.fetchRow(row, rowBuffer);
71
            this.putRow(row, rowBuffer);
72
        }
73
    }
74

    
75
    protected void loadPage(int row) {
76
        if (loaded && row >= firstRowOfPage
77
            && row < firstRowOfPage + rowsPerPage) {
78
            return;
79
        }
80
        loaded=false;
81

    
82
        saveCurrentPage();
83
        int currentPage = row / rowsPerPage; //Divisi?n entera
84
        firstRowOfPage = currentPage * rowsPerPage;
85
        try {
86
            int rowsInPage = rowsPerPage;
87
            if(firstRowOfPage + rowsPerPage > this.rows){
88
                rowsInPage = this.rows - firstRowOfPage;
89
            }
90
            this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType());
91
            loaded=true;
92
        } catch (IOException e) {
93
            throw new RuntimeException("Can't load current page", e);
94
        }
95
    }
96

    
97
    protected void saveCurrentPage() {
98
        try {
99
            int rowsInPage = rowsPerPage;
100
            if(firstRowOfPage + rowsPerPage > this.rows){
101
                rowsInPage = this.rows - firstRowOfPage;
102
            }
103
            this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType());
104
        } catch (UnsupportedOperationException e) {
105
            // Do nothing, operation not supported
106
        } catch (IOException e) {
107
            throw new RuntimeException("Can't save current page", e);
108
        }
109
    }
110

    
111
    protected abstract int getDataSize();
112

    
113
    @Override
114
    public boolean isReadOnly() {
115
        if(this.pageManager==null){
116
            return false;
117
        }
118
        return !this.pageManager.isSupportedSave();
119
    }
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
}