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

History | View | Annotate | Download (5.77 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
    protected static final int MAX_PREFERED_SIZE = 2621440; // 2.5MB;
35

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

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

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

    
66
    }
67

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

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

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

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

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

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

    
137
    protected abstract int getDataSize();
138

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

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

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

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

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

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

    
181

    
182
}