Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / AbstractPaginatedBand.java @ 43803

History | View | Annotate | Download (5.7 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.dispose.DisposeUtils;
18
import org.gvsig.tools.exception.BaseException;
19

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

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

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

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

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

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

    
65
    }
66

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

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

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

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

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

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

    
136
    protected abstract int getDataSize();
137

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

    
146
    @Override
147
    public boolean isPaginated() {
148
        return true;
149
    }
150

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

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

    
167
    @Override
168
    public void doDispose() throws BaseException {
169
        super.doDispose();
170
        data = null;
171
        DisposeUtils.dispose(pageManager);
172
        pageManager = null;
173
    }
174

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

    
180

    
181
}