Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / stripecache / HDDPage.java @ 991

History | View | Annotate | Download (4.51 KB)

1
package org.gvsig.raster.cache.buffer.impl.stripecache;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6

    
7
import org.gvsig.raster.cache.buffer.BufferDataSource;
8
import org.gvsig.raster.cache.buffer.exception.WrongParameterException;
9
import org.gvsig.raster.cache.buffer.impl.BufferServices;
10

    
11

    
12
/**
13
 * Esta clase representa una p?gina de disco. Una p?gina de disco tendr? un n?mero de
14
 * p?gina asociado y un servidor de datos de disco por cada banda. 
15
 * 
16
 * @author Nacho Brodin (nachobrodin@gmail.com)
17
 */
18
public class HDDPage {
19
        private String              timePag    = null;    
20
        private int                 iPag       = -1;
21
        private int                 nBands     = -1;
22
        private BufferDataSource    dataSource = null;
23
                
24
        /**
25
         * Constructor: crea un objeto dataSource para cada banda. Para todos los datasource de un 
26
         * mismo HddPage se usa el mismo identificador.
27
         * @param nPag N?mero de p?gina
28
         * @param nBands N?mero de bandas del dataset
29
         */
30
        public HDDPage(int iPag, int nBands, BufferDataSource dataSource) {
31
                this.dataSource = dataSource;
32
                this.iPag = iPag;
33
                this.nBands = nBands;
34
                this.timePag = BufferServices.getTemporalPath() + File.separator + Long.toString(System.currentTimeMillis()) + "-" + iPag + "-";
35
        }
36
                
37
        /**
38
         * Limpia los trozos de cach? en disco. Despu?s del llamar a este 
39
         * m?todo no puede volver a usarse esta cach?.
40
         * @throws IOException 
41
         */
42
        public void deletePage() throws IOException {
43
                for (int i = 0; i < nBands; i++) 
44
                        dataSource.delete(timePag + i);
45
        }
46
        
47
        /**
48
         * Limpia los trozos de cach? en disco de una banda. Despu?s del llamar a este 
49
         * m?todo no puede volver a usarse esta banda.
50
         * @throws IOException 
51
         */
52
        public void removeBand(int pos) throws IOException {
53
                dataSource.delete(timePag + pos);
54
                for (int i = (pos + 1); i < nBands; i++) 
55
                        dataSource.changeFileName(timePag + i, timePag + (i - 1));
56
                nBands --;
57
        }
58
        
59
        /**
60
         * Adds a new band in the selected position. 
61
         * @param pos
62
         * @throws IOException 
63
         */
64
        public void addBand(int pos, PageBuffer pb) throws IOException {
65
                for (int i = (nBands - 1); i >= pos ; i--)
66
                        dataSource.changeFileName(timePag + i, timePag + (i + 1));
67
                dataSource.saveBand(pb.getBand(0), timePag + pos);
68
                nBands ++;
69
        }
70
        
71
        /**
72
         * Swaps bands according to positions selected in bandPosition parameter.
73
         * 
74
         * @param  bandPosition
75
         *         New order for the bands in the array. Each position in the array represent the old band
76
         *         .The number that it contains represent the new position of that band. For instance, an
77
         *         array with values {1, 0, 2, 3} means that it has four bands and the band number one is swaped
78
         *         with the band number zero. The bands two and three don't change its position in this example.                           
79
         * @throws WrongParameterException
80
         */
81
         public void swapBands(int[] bandPosition) throws WrongParameterException {
82
                 for (int i = 0; i < bandPosition.length; i++) 
83
                         dataSource.changeFileName(timePag + i, timePag + i + "old");
84
                 for (int i = 0; i < bandPosition.length; i++) 
85
                         dataSource.changeFileName(timePag + bandPosition[i] + "old", timePag + i);
86
         }
87
        
88
        /**
89
         * Loads a page of data from the hard disk. 
90
         * @param buf Buffer where is loaded
91
         * @throws InterruptedException
92
         */
93
        public void loadPage(PageBuffer buf) throws InterruptedException {
94
                dataSource.loadPage(buf, timePag);
95
        }
96
        
97
        /**
98
         * Salva una p?gina especificada en el par?metro nPag a disco. 
99
         * Para esto recorre todas las bandas de la p?gina llamando al save de cada una.
100
         *   
101
         * @param nPag N?mero de p?gina a salvar
102
         * @throws IOException
103
         */
104
        public void savePage(PageBuffer buf) throws IOException {
105
                dataSource.savePage(buf, timePag);
106
        }
107

    
108
        /**
109
         * Gets the string with the file id
110
         * @return
111
         */
112
        public String getFileID() {
113
                return timePag;
114
        }
115
        
116
        /**
117
         * Sets the string with the file id
118
         * @return
119
         */
120
        public void setFileID(String id) {
121
                this.timePag = id;
122
        }
123
        
124
        /**
125
         * This method creates a new file with a new identifier and copy data. 
126
         */
127
        public Object clone() {
128
                HDDPage clon = new HDDPage(iPag, nBands, dataSource);
129
                String newID = BufferServices.getTemporalPath() + File.separator + Long.toString(System.currentTimeMillis()) + "-" + iPag + "-";
130
                clon.setFileID(newID);
131
                for (int i = 0; i < nBands; i++) {
132
                        try {
133
                                File f = new File(timePag + i);
134
                                if(f.exists() && f.canRead() && f.canWrite())
135
                                        BufferServices.copyFile(timePag + i, newID + i);
136
                        } catch (FileNotFoundException e) {
137
                                e.printStackTrace();
138
                        } catch (IOException e) {
139
                                e.printStackTrace();
140
                        }
141
                }
142
                return clon;
143
        }
144
}