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

History | View | Annotate | Download (5.62 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.BufferNotification;
15
import org.gvsig.raster.lib.buffer.api.NoData;
16
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException;
17
import org.gvsig.tools.exception.BaseException;
18

    
19
/**
20
 * @author fdiaz
21
 *
22
 */
23
public abstract class AbstractPaginatedBand extends AbstractBand implements Band {
24

    
25
    private static final Logger logger = LoggerFactory.getLogger(AbstractPaginatedBand.class);
26
    protected Buffer data;
27
    protected int firstRowOfPage;
28
    protected int rowsPerPage;
29
    protected BandPageManager pageManager;
30
    private boolean loaded;
31

    
32
    protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
33

    
34
    /**
35
     * @param rows
36
     * @param columns
37
     * @param noData
38
     * @param pageManager
39
     */
40
    public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) {
41
        this.rows = rows;
42
        this.columns = columns;
43
        this.loaded = false;
44
        calculateRowsPerPage();
45

    
46
        if (noData == null) {
47
            this.noData = BufferLocator.getBufferManager().createNoData(null, null);
48
        } else {
49
            this.noData = noData;
50
        }
51
        this.pageManager = pageManager;
52
    }
53

    
54
    private void calculateRowsPerPage() {
55
        rowsPerPage = MAX_PREFERED_SIZE / (this.columns * getDataSize());
56
        if (rowsPerPage < 1) {
57
            rowsPerPage = 1;
58
        }
59
        if (rowsPerPage > rows) {
60
            rowsPerPage = rows;
61
        }
62

    
63
    }
64

    
65
    @Override
66
    public void copyFrom(Band source) throws CopyFromBandException {
67
        doCopyFrom(source);
68
        notifyObservers(new DefaultBandNotification(BandNotification.COPY_FROM, new Object[] { source }));
69
    }
70

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

    
83
    @Override
84
    protected void doCopyFrom(Band source, int row, int column) throws CopyFromBandException {
85
        if (this.getColumns() <= row+source.getColumns() || this.getRows() <= column+source.getRows()
86
            || this.getDataType()!=source.getDataType() ) {
87
            throw new CopyFromBandException(source, this);
88
        }
89
        //FIXME:
90
        Object rowBuffer = source.createRowBuffer();
91
        for(int r=0; r<Math.min(this.getRows()-row, source.getRows()); r++){
92
            source.fetchRow(r, rowBuffer);
93
            System.arraycopy(rowBuffer, 0, this.data, (r+row)*this.getColumns()+column, Math.min(this.getColumns()-column, source.getColumns()));
94
            this.putRow(r, rowBuffer);
95
        }
96
    }
97

    
98
    protected void loadPage(int row) {
99
        if (loaded && row >= firstRowOfPage && row < firstRowOfPage + rowsPerPage) {
100
            return;
101
        }
102
        loaded = false;
103

    
104
        saveCurrentPage();
105
        int currentPage = row / rowsPerPage; // Divisi?n entera
106
        firstRowOfPage = currentPage * rowsPerPage;
107
        try {
108
            int rowsInPage = rowsPerPage;
109
            if (firstRowOfPage + rowsPerPage > this.rows) {
110
                rowsInPage = this.rows - firstRowOfPage;
111
            }
112
            this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType());
113
            loaded = true;
114
            this.notifyObservers(new DefaultBufferNotification(BandNotification.LOADED_PAGE, new Object[] { this }));
115
        } catch (IOException e) {
116
            throw new RuntimeException("Can't load current page", e);
117
        }
118
    }
119

    
120
    protected void saveCurrentPage() {
121
        try {
122
            int rowsInPage = rowsPerPage;
123
            if (firstRowOfPage + rowsPerPage > this.rows) {
124
                rowsInPage = this.rows - firstRowOfPage;
125
            }
126
            this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType());
127
        } catch (UnsupportedOperationException e) {
128
            // Do nothing, operation not supported
129
        } catch (IOException e) {
130
            throw new RuntimeException("Can't save current page", e);
131
        }
132
    }
133

    
134
    protected abstract int getDataSize();
135

    
136
    @Override
137
    public boolean isReadOnly() {
138
        if (this.pageManager == null) {
139
            return false;
140
        }
141
        return !this.pageManager.isSupportedSave();
142
    }
143

    
144
    @Override
145
    public boolean isPaginated() {
146
        return true;
147
    }
148

    
149
    @Override
150
    public BandInfo getBandInfo() {
151
        return this.pageManager == null ? null : this.pageManager.getBandInfo();
152
    }
153

    
154
    /**
155
     * Calculate buffer size than must be allocated.
156
     *
157
     * @param rows
158
     * @param columns
159
     * @return
160
     */
161
    protected int calculateBufferSizeToAllocate(int rows, int columns) {
162
        return rowsPerPage * columns;
163
    }
164

    
165
    @Override
166
    public void doDispose() throws BaseException {
167
        super.doDispose();
168
        data = null;
169
        //FIXME: ?hacer disposables los pageManagers?
170
        pageManager = null;
171
    }
172

    
173
    @Override
174
    protected void finalize() throws Throwable {
175
        super.finalize();
176
    }
177

    
178

    
179
}