Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / buffer / cache / RasterCache.java @ 1029

History | View | Annotate | Download (25.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
package org.gvsig.raster.impl.buffer.cache;
24

    
25
import java.io.File;
26
import java.io.FileNotFoundException;
27
import java.io.IOException;
28

    
29
import org.gvsig.fmap.dal.coverage.RasterLibrary;
30
import org.gvsig.fmap.dal.coverage.RasterLocator;
31
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
32
import org.gvsig.fmap.dal.coverage.datastruct.Band;
33
import org.gvsig.fmap.dal.coverage.util.FileUtils;
34
import org.gvsig.raster.impl.buffer.RasterBand;
35
import org.gvsig.raster.impl.buffer.RasterBuffer;
36

    
37
/*
38
 * TODO: OPTIMIZACION: Trabajo de optimizaci?n en la velocidad e acceso a cach?.
39
 * TODO: FUNCIONALIDAD: Acabar de implementar los m?todos de acceso a datos, intercambio de bandas, etc...
40
 */
41
/**
42
 * Esta clase representa un buffer de datos cacheado. El estar en cache significa que
43
 * solo una parte de los datos estar?n en memoria y que gestiona, dependiendo de las
44
 * peticiones, que partes hay que cargar a memoria y en que momento. Para esto el buffer
45
 * no se llena de golpe sino que utiliza una clase que sirve los datos desde la fuente. 
46
 * Esta clase servidora de datos debe implementar el interfaz ICacheDatasetSourcepara 
47
 * servir datos de la forma requerida.
48
 *   
49
 * @author Nacho Brodin (nachobrodin@gmail.com)
50
 *
51
 */
52
public class RasterCache extends RasterBuffer {
53

    
54
        private Cache                         cache    = null;
55
        private LRUAlgorithm          lru      = null;
56
        private int             lastLine = -1;
57
        
58
        //TODO: FUNCIONALIDAD: Intercambio de bandas para el buffer cacheado
59
        
60
         public class CacheBand extends RasterBand {
61
                 public ICacheDataSource[] cacheDataServer = null;
62
                 
63
                 public CacheBand(int height, int width){
64
                         super(height, width);
65
                 }
66
                 
67
                 public Object getLine(int line) {
68
                         return null;
69
                 }
70
                 
71
                 public void setLine(int line, Object value){
72
                        
73
                 }
74
                 
75
                 public Object getBuf(){
76
                         return null;
77
                 }
78
                 
79
                 public void setFileName(int numBand){
80
                        for (int i = 0; i < cacheDataServer.length; i++) 
81
                                ((CacheDataServer)cacheDataServer[i]).setName(null, numBand, i);
82
                 }
83
        }
84
         
85
    /**
86
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
87
     * la cach? con los datos pasados.
88
     * @param dataType Tipo de dato del buffer
89
     * @param width Ancho del buffer
90
     * @param height Alto del buffer
91
     * @param nBands N?mero de bandas del buffer
92
     */
93
        public RasterCache(int dataType, int width, int height, int nBands){
94
                cache = new Cache(nBands, dataType, width, height);
95
                lru = new LRUAlgorithm(cache);
96
                
97
            this.dataType = dataType;
98
        this.width = width;
99
        this.height = height;
100
        this.nBands = nBands;
101
        }
102
        
103
        /**
104
         * Limpia los trozos de cach? en disco. Despu?s del llamar a este 
105
         * m?todo no puede volver a usarse esta cach?.
106
         * @throws IOException 
107
         */
108
        public void clearCache() throws IOException {
109
                cache.clearCache(this.nBands);
110
        }
111
        
112
        /**
113
         * Borra la cach? cuando se elimina el objeto RasterCache ya que
114
         * los trozos ya no pueden ser referenciados.
115
         */
116
        protected void finalize() throws Throwable {
117
                free();
118
        }
119
        
120
        /*
121
     * (non-Javadoc)
122
     * @see org.gvsig.raster.driver.Buffer#isBandSwitchable()
123
     */
124
    public boolean isBandSwitchable(){
125
            return true;
126
    }
127
    
128
        public void malloc(int dataType, int width, int height, int bandNr) {
129
        }
130

    
131
        /**
132
         * Obtiene la cach?
133
         * @return
134
         */
135
        public Cache getCache() {
136
                return cache;
137
        }
138
        
139
        /**
140
         * Asigna la cache
141
         * @param cache
142
         */
143
        public void setCache(Cache cache) {
144
                this.cache = cache;
145
                this.lru.setCache(cache);
146
        }
147
        
148
//*********************************************************
149
        
150
        public byte[][] getLineByte(int line) {
151
        if (exists(line)) {
152
            return cache.getAccessPage()
153
                .getLineByte((line & cache.getOffset()));
154
        }
155
        return null;
156
        }
157

    
158
        public short[][] getLineShort(int line) {
159
        if (exists(line)) {
160
            return cache.getAccessPage().getLineShort(
161
                (line & cache.getOffset()));
162
        }
163
        return null;
164
        }
165

    
166
        public int[][] getLineInt(int line) {
167
        if (exists(line)) {
168
            return cache.getAccessPage().getLineInt((line & cache.getOffset()));
169
        }
170
        return null;
171
        }
172

    
173
        public float[][] getLineFloat(int line) {
174
        if (exists(line)) {
175
            return cache.getAccessPage().getLineFloat(
176
                (line & cache.getOffset()));
177
        }
178
        return null;
179
        }
180

    
181
        public double[][] getLineDouble(int line) {
182
        if (exists(line)) {
183
            return cache.getAccessPage().getLineDouble(
184
                (line & cache.getOffset()));
185
        }
186
        return null;
187
        }
188

    
189
        //*********************************************************
190
        
191
        public void setLineByte(byte[][] data, int line) {
192
        if (exists(line)) {
193
            cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
194
        }
195
        }
196

    
197
        public void setLineShort(short[][] data, int line) {
198
        if (exists(line)) {
199
            cache.getAccessPage()
200
                .setLineShort(data, (line & cache.getOffset()));
201
        }
202
        }
203

    
204
        public void setLineInt(int[][] data, int line) {
205
        if (exists(line)) {
206
            cache.getAccessPage().setLineInt(data, (line & cache.getOffset()));
207
        }
208
        }
209

    
210
        public void setLineFloat(float[][] data, int line) {
211
        if (exists(line)) {
212
            cache.getAccessPage()
213
                .setLineFloat(data, (line & cache.getOffset()));
214
        }
215
        }
216

    
217
        public void setLineDouble(double[][] data, int line) {
218
        if (exists(line)) {
219
            cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
220
        }
221
        }
222

    
223
        //*********************************************************
224
        
225
        public byte[] getLineFromBandByte(int line, int band) {
226
        if (exists(line)) {
227
            return cache.getAccessPage().getLineFromBandByte(
228
                (line & cache.getOffset()), band);
229
        }
230
        return null;
231
        }
232

    
233
        public short[] getLineFromBandShort(int line, int band) {
234
        if (exists(line)) {
235
            return cache.getAccessPage().getLineFromBandShort(
236
                (line & cache.getOffset()), band);
237
        }
238
        return null;
239
        }
240

    
241
        public int[] getLineFromBandInt(int line, int band) {
242
        if (exists(line)) {
243
            return cache.getAccessPage().getLineFromBandInt(
244
                (line & cache.getOffset()), band);
245
        }
246
        return null;
247
        }
248

    
249
        public float[] getLineFromBandFloat(int line, int band) {
250
        if (exists(line)) {
251
            return cache.getAccessPage().getLineFromBandFloat(
252
                (line & cache.getOffset()), band);
253
        }
254
        return null;
255
        }
256

    
257
        public double[] getLineFromBandDouble(int line, int band) {
258
        if (exists(line)) {
259
            return cache.getAccessPage().getLineFromBandDouble(
260
                (line & cache.getOffset()), band);
261
        }
262
        return null;
263
        }
264

    
265
        //*********************************************************
266
        
267
        public void setLineInBandByte(byte[] data, int line, int band) {
268
        if (exists(line)) {
269
            cache.getAccessPage().setLineInBandByte(data,
270
                (line & cache.getOffset()), band);
271
        }
272
        }
273

    
274
        public void setLineInBandShort(short[] data, int line, int band) {
275
        if (exists(line)) {
276
            cache.getAccessPage().setLineInBandShort(data,
277
                (line & cache.getOffset()), band);
278
        }
279
        }
280

    
281
        public void setLineInBandInt(int[] data, int line, int band) {
282
        if (exists(line)) {
283
            cache.getAccessPage().setLineInBandInt(data,
284
                (line & cache.getOffset()), band);
285
        }
286
        }
287

    
288
        public void setLineInBandFloat(float[] data, int line, int band) {
289
        if (exists(line)) {
290
            cache.getAccessPage().setLineInBandFloat(data,
291
                (line & cache.getOffset()), band);
292
        }
293
        }
294

    
295
        public void setLineInBandDouble(double[] data, int line, int band) {
296
        if (exists(line)) {
297
            cache.getAccessPage().setLineInBandDouble(data,
298
                (line & cache.getOffset()), band);
299
        }
300
        }
301

    
302
        //*********************************************************
303
        
304
        public byte getElemByte(int line, int col, int band) {
305
        if (exists(line)) {
306
            return cache.getAccessPage().getElemByte(
307
                (line & cache.getOffset()), col, band);
308
                }
309
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().byteValue() : RasterLibrary.defaultByteNoDataValue; // No leemos el dato
310
        }
311

    
312
        public short getElemShort(int line, int col, int band) {
313
        if (exists(line)) {
314
            return cache.getAccessPage().getElemShort(
315
                (line & cache.getOffset()), col, band);
316
                }
317
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().shortValue() : RasterLibrary.defaultShortNoDataValue; // No leemos el dato
318
        }
319

    
320
        public int getElemInt(int line, int col, int band) {
321
        if (exists(line)) {
322
            return cache.getAccessPage().getElemInt((line & cache.getOffset()),
323
                col, band);
324
                }
325
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().intValue() : RasterLibrary.defaultIntegerNoDataValue; // No leemos el dato
326
        }
327

    
328
        public float getElemFloat(int line, int col, int band) {
329
        if (exists(line)) {
330
            return cache.getAccessPage().getElemFloat(
331
                (line & cache.getOffset()), col, band);
332
                }
333
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().floatValue() : RasterLibrary.defaultFloatNoDataValue; // No leemos el dato
334
        }
335

    
336
        public double getElemDouble(int line, int col, int band) {
337
        if (exists(line)) {
338
            return cache.getAccessPage().getElemDouble(
339
                (line & cache.getOffset()), col, band);
340
                }
341
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue; // No leemos el dato
342
        }
343

    
344
        //*********************************************************
345
        
346
        public void setElem(int line, int col, int band, byte data) {
347
        if (exists(line)) {
348
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
349
                band, data);
350
        }
351
        }
352

    
353
        public void setElem(int line, int col, int band, short data) {
354
        if (exists(line)) {
355
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
356
                band, data);
357
        }
358
        }
359

    
360
        public void setElem(int line, int col, int band, int data) {
361
        if (exists(line)) {
362
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
363
                band, data);
364
        }
365
        }
366

    
367
        public void setElem(int line, int col, int band, float data) {
368
        if (exists(line)) {
369
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
370
                band, data);
371
        }
372
        }
373

    
374
        public void setElem(int line, int col, int band, double data) {
375
        if (exists(line)) {
376
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
377
                band, data);
378
        }
379
        }
380
        
381
        //*********************************************************
382

    
383
        public void getElemByte(int line, int col, byte[] data) {
384
        if (!exists(line)) {
385
                        for (int iBand = 0; iBand < data.length; iBand++)
386
                    data[iBand] =  getNoDataValue().isDefined() ? getNoDataValue().getValue().byteValue() : RasterLibrary.defaultByteNoDataValue;
387
                }
388
                cache.getAccessPage().getElemByte((line & cache.getOffset()), col, data);
389
        }
390

    
391
        public void getElemShort(int line, int col, short[] data) {
392
        if (!exists(line)) {
393
                        for (int iBand = 0; iBand < data.length; iBand++)
394
                    data[iBand] =  getNoDataValue().isDefined() ? getNoDataValue().getValue().shortValue() : RasterLibrary.defaultShortNoDataValue;
395
                }
396
                cache.getAccessPage().getElemShort((line & cache.getOffset()), col, data);
397
        }
398

    
399
        public void getElemInt(int line, int col, int[] data) {
400
        if (!exists(line)) {
401
                        for (int iBand = 0; iBand < data.length; iBand++)
402
                    data[iBand] =  getNoDataValue().isDefined() ? getNoDataValue().getValue().intValue() : RasterLibrary.defaultIntegerNoDataValue;
403
                }
404
                cache.getAccessPage().getElemInt((line & cache.getOffset()), col, data);
405
        }
406

    
407
        public void getElemFloat(int line, int col, float[] data) {
408
        if (!exists(line)) {
409
                        for (int iBand = 0; iBand < data.length; iBand++)
410
                    data[iBand] = getNoDataValue().isDefined() ? getNoDataValue().getValue().floatValue() : RasterLibrary.defaultFloatNoDataValue;;
411
                }
412
                cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, data);
413
        }
414

    
415
        public void getElemDouble(int line, int col, double[] data) {
416
        if (!exists(line)) {
417
                        for (int iBand = 0; iBand < data.length; iBand++)
418
                    data[iBand] =  getNoDataValue().isDefined() ? getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue;;
419
                }
420
                cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, data);
421
        }
422
        
423
        //*********************************************************
424
        
425
    private boolean exists(int line) {
426
        // Store the last line checked so we don't check it again for each
427
        // pixel of the same line
428
        if (line != lastLine) {
429
            try {
430
                lru.cacheAccess(line, false);
431
                this.lastLine = line;
432
            } catch (InvalidPageNumberException e) {
433
                return false;
434
            }
435
        }
436
        return true;
437
    }
438

    
439
    public void setElemByte(int line, int col, byte[] data) {
440
        if (exists(line)) {
441
            cache.getAccessPage().setElemByte((line & cache.getOffset()), col,
442
                data);
443
        }
444
    }
445

    
446
    public void setElemShort(int line, int col, short[] data) {
447
        if (exists(line)) {
448
            cache.getAccessPage().setElemShort((line & cache.getOffset()), col,
449
                data);
450
        }
451
    }
452

    
453
    public void setElemInt(int line, int col, int[] data) {
454
        if (exists(line)) {
455
            cache.getAccessPage().setElemInt((line & cache.getOffset()), col,
456
                data);
457
        }
458
    }
459

    
460
    public void setElemFloat(int line, int col, float[] data) {
461
        if (exists(line)) {
462
            cache.getAccessPage().setElemFloat((line & cache.getOffset()), col,
463
                data);
464
        }
465
    }
466

    
467
    public void setElemDouble(int line, int col, double[] data) {
468
        if (exists(line)) {
469
            cache.getAccessPage().setElemDouble((line & cache.getOffset()),
470
                col, data);
471
        }
472
    }
473

    
474
    /*
475
     *  (non-Javadoc)
476
     * @see org.gvsig.fmap.driver.Buffer#getBandBuffer(int)
477
     */
478
    public Buffer getBandBuffer(int Band){
479
            RasterCache rasterCache = new RasterCache(getDataType(), getWidth(), getHeight(), 1);
480
            CacheStruct cs = new CacheStruct();
481
            cs.setHPag(cache.getCacheStruct().getHPag());
482
            cs.setOffset(cache.getCacheStruct().getOffset());
483
            cs.setNPags(cache.getCacheStruct().getNPags());
484
            cs.setNBands(1);
485
            cs.setNGroups(cache.getCacheStruct().getNGroups());
486
            cs.setBitsPag(cache.getCacheStruct().getBitsPag());
487
            cs.setNTotalPags((int)(getHeight() / cache.getCacheStruct().getHPag()));
488
            cs.setDataType(cache.getCacheStruct().getDataType());
489
            
490
            Cache c = new Cache(cs, getWidth());
491
            rasterCache.setCache(c);
492
            
493
            Band band = getBand(Band);
494
            rasterCache.assignBand(0, band);
495
            return rasterCache;
496
    }
497
    
498
    /*
499
     *  (non-Javadoc)
500
     * @see org.gvsig.fmap.driver.Buffer#replicateBand(int, int)
501
     */
502
        public void replicateBand(int orig, int dest) {
503
        }
504
                
505
        /*
506
     *  (non-Javadoc)
507
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
508
     */
509
    public void switchBands(int[] bandPosition){
510
            
511
    }
512

    
513
        //*********************************************************
514
        
515
        public void assign(int band, byte value) {
516
                for(int line = 0; line < height; line ++){
517
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
518
                    for(int col = 0; col < width; col ++){
519
                            try {
520
                                    if(beginLine){
521
                                            lru.cacheAccess(line, false);
522
                                            beginLine = false;
523
                                    }
524
                            } catch (InvalidPageNumberException e) {return;}
525
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
526
                    }
527
                }
528
        }
529

    
530
        public void assign(int band, short value) {
531
                for(int line = 0; line < height; line ++){
532
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
533
                    for(int col = 0; col < width; col ++){
534
                            try {
535
                                    if(beginLine){
536
                                            lru.cacheAccess(line, false);
537
                                            beginLine = false;
538
                                    }
539
                            } catch (InvalidPageNumberException e) {return;}
540
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
541
                    }
542
                }                
543
        }
544

    
545
        public void assign(int band, int value) {
546
                for(int line = 0; line < height; line ++){
547
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
548
                    for(int col = 0; col < width; col ++){
549
                            try {
550
                                    if(beginLine){
551
                                            lru.cacheAccess(line, false);
552
                                            beginLine = false;
553
                                    }
554
                            } catch (InvalidPageNumberException e) {return;}
555
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
556
                    }
557
                }        
558
        }
559

    
560
        public void assign(int band, float value) {
561
                for(int line = 0; line < height; line ++){
562
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
563
                    for(int col = 0; col < width; col ++){
564
                            try {
565
                                    if(beginLine){
566
                                            lru.cacheAccess(line, false);
567
                                            beginLine = false;
568
                                    }
569
                            } catch (InvalidPageNumberException e) {return;}
570
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
571
                    }
572
                }        
573
        }
574

    
575
        public void assign(int band, double value) {
576
                for(int line = 0; line < height; line ++){
577
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
578
                    for(int col = 0; col < width; col ++){
579
                            try {
580
                                    if(beginLine){
581
                                            lru.cacheAccess(line, false);
582
                                            beginLine = false;
583
                                    }
584
                            } catch (InvalidPageNumberException e) {return;}
585
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
586
                    }
587
                }
588
        }
589
    
590
    /*
591
     *  (non-Javadoc)
592
     * @see org.gvsig.fmap.driver.Buffer#cloneBuffer()
593
     */
594
    public Buffer cloneBuffer(){
595
            return null;
596
    }
597
    
598
    /*
599
     * (non-Javadoc)
600
     * @see org.gvsig.fmap.driver.Buffer#interchangeBands(int, int)
601
     */
602
        public void interchangeBands(int band1, int band2) {
603
                Band b1 = getBand(band1);
604
                Band b2 = getBand(band2);
605
                
606
                try {
607
                        cache.assignBand(band2, ((CacheBand)b1).cacheDataServer);
608
                        cache.assignBand(band1, ((CacheBand)b2).cacheDataServer);
609
                } catch (IOException e) {
610
                        e.printStackTrace();
611
                }
612
                
613
        }
614
            
615
    /*
616
     *  (non-Javadoc)
617
     * @see org.gvsig.fmap.driver.Buffer#mallocOneBand(int, int, int, int)
618
     */
619
    public void mallocOneBand(int dataType, int width, int height, int band) {
620
                        
621
        }
622

    
623
    /*
624
         * (non-Javadoc)
625
         * @see org.gvsig.raster.driver.Buffer#getBand(int)
626
         */
627
    public Band getBand(int band) {
628
            CacheBand cb = new CacheBand(getHeight(), getWidth());
629
            cb.cacheDataServer = new CacheDataServer[cache.getNTotalPags()];
630
            
631
            try {
632
                        cache.resetCache();
633
                } catch (IOException e) {
634
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
635
                        return null;
636
                }
637
            for (int iPage = 0; iPage < cache.getNTotalPags(); iPage++)
638
                    cb.cacheDataServer[iPage] = cache.getHddPage(iPage, band);        
639
                    
640
            return cb;
641
    }
642
    
643
    /*
644
     *  (non-Javadoc)
645
     * @see org.gvsig.fmap.driver.Buffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.Band)
646
     */
647
        public void copyBand(int nBand, Band band) {
648
                FileUtils file = RasterLocator.getManager().getFileUtils();
649
                if(band instanceof CacheBand){
650
                        CacheBand cb = new CacheBand(band.getHeight(), band.getWidth());
651
                        cb.cacheDataServer = new CacheDataServer[((CacheBand)band).cacheDataServer.length];
652
                        
653
                        for (int iPage = 0; iPage < cb.cacheDataServer.length; iPage++) {
654
                                cb.cacheDataServer[iPage] = new CacheDataServer(null, nBand, iPage);
655
                                String path = ((CacheBand)band).cacheDataServer[iPage].getPath();
656
                                File f = new File(path);
657
                                if(f.exists()){
658
                                        try {
659
                                                file.copyFile(path, cb.cacheDataServer[iPage].getPath());
660
                                        } catch (FileNotFoundException e) {
661
                                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
662
                                                e.printStackTrace();
663
                                        } catch (IOException e) {
664
                                                e.printStackTrace();
665
                                        }
666
                                }
667
                        }
668
                        try {
669
                                cache.deleteBand(nBand);
670
                                cache.assignBand(nBand, cb.cacheDataServer);
671
                        } catch (IOException e) {
672
                                e.printStackTrace();
673
                        }
674
                                                
675
                }                
676
        }
677

    
678
        /*
679
         *  (non-Javadoc)
680
         * @see org.gvsig.fmap.driver.Buffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.Band)
681
         */
682
        public void assignBand(int nBand, Band band) {
683
                if(band instanceof CacheBand) {
684
                        //((CacheBand)band).setFileName(nBand);
685
                        try {
686
                                cache.deleteBand(nBand);
687
                                cache.assignBand(nBand, ((CacheBand)band).cacheDataServer);
688
                        } catch (IOException e) {
689
                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
690
                                return;
691
                        }
692
                        
693
                }
694
        }
695

    
696
        /*
697
         *  (non-Javadoc)
698
         * @see org.gvsig.fmap.driver.Buffer#createBand(byte)
699
         */
700
        public Band createBand(byte defaultValue) {
701
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
702
                Band band = null;
703
                try {
704
                        band = createBand(pageBuffer);
705
                } catch (IOException e) {
706
                        return null;
707
                }
708
                loadPage(new Byte(defaultValue), pageBuffer);
709
                return band;
710
        }
711
                
712
        /*
713
         *  (non-Javadoc)
714
         * @see org.gvsig.fmap.driver.Buffer#assignBandToNotValid(int)
715
         */
716
        public void assignBandToNotValid(int Band) {
717
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
718

    
719
                switch(getDataType()){
720
            case Buffer.TYPE_BYTE:         loadPage(new Byte((byte)getNotValidValue()), pageBuffer);break;
721
            case Buffer.TYPE_SHORT:        loadPage(new Short((short)getNotValidValue()), pageBuffer);break;
722
            case Buffer.TYPE_INT:                loadPage(new Integer((int)getNotValidValue()), pageBuffer);break;
723
            case Buffer.TYPE_FLOAT:        loadPage(new Float((float)getNotValidValue()), pageBuffer);break;
724
            case Buffer.TYPE_DOUBLE:        loadPage(new Double((double)getNotValidValue()), pageBuffer);break;
725
            }        
726
                
727
                try {
728
                        CacheBand cb = (CacheBand)createBand(pageBuffer);
729
                        cb.setFileName(Band);
730
                        assignBand(Band, cb);
731
                } catch (IOException e) {
732
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
733
                        e.printStackTrace();
734
                }
735
        }
736

    
737
        /**
738
         * Creaci?n de una banda a partir de una pagina de cache cargada con datos.
739
         * @param pageBuffer Pagina de cache cargada de datos
740
         * @param numBand N?mero de banda a la que representa
741
         * @return Band
742
         * @throws IOException
743
         */
744
        private Band createBand(PageBandBuffer pageBuffer) throws IOException{
745
                CacheDataServer[] ds = new CacheDataServer[cache.getNTotalPags()];
746
                for (int i = 0; i < cache.getNTotalPags(); i++) {
747
                        ds[i] = new CacheDataServer(null, 0, i);
748
                        ds[i].savePage(pageBuffer);
749
                }
750
                CacheBand band = new CacheBand(getHeight(), getWidth());
751
                band.cacheDataServer = ds;
752
                return band;
753
        }
754
        
755
        /**
756
         * Carga la p?gina con el valor pasado por par?metro. El valor ser? del mismo tipo de dato 
757
         * que el buffer actual.
758
         * @param value Valor a inicializar
759
         * @param pageBuffer P?gina
760
         */
761
        private void loadPage(Object value, PageBandBuffer pageBuffer){
762
                switch(getDataType()){
763
            case Buffer.TYPE_BYTE: for(int i = 0 ; i < getWidth(); i ++)
764
                                                                    for(int j = 0 ; j < cache.getHPag(); j ++)
765
                                                                            pageBuffer.setElem(j, i, 0, ((Byte)value).byteValue());
766
                                                            break;
767
            case Buffer.TYPE_SHORT: for(int i = 0 ; i < getWidth(); i ++)
768
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
769
                                                                                pageBuffer.setElem(j, i, 0, ((Short)value).shortValue());
770
                                                                 break;
771
            case Buffer.TYPE_INT:        for(int i = 0 ; i < getWidth(); i ++)
772
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
773
                                                                                pageBuffer.setElem(j, i, 0, ((Integer)value).intValue());
774
                                                                break;
775
            case Buffer.TYPE_FLOAT:for(int i = 0 ; i < getWidth(); i ++)
776
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
777
                                                                                pageBuffer.setElem(j, i, 0, ((Float)value).floatValue());
778
                                                                        break;
779
            case Buffer.TYPE_DOUBLE:for(int i = 0 ; i < getWidth(); i ++)
780
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
781
                                                                                pageBuffer.setElem(j, i, 0, ((Double)value).doubleValue());
782
                                                                 break;
783
            }        
784
        }
785
        
786
        /*
787
         * (non-Javadoc)
788
         * @see org.gvsig.fmap.dal.coverage.dataset.Buffer#isReadOnlyBuffer()
789
         */
790
        public boolean isReadOnlyBuffer() {
791
                return false;
792
        }
793
        
794
        /*
795
         * (non-Javadoc)
796
         * @see org.gvsig.fmap.dal.coverage.dataset.Buffer#isCached()
797
         */
798
        public boolean isCached() {
799
                return true;
800
        }
801
        
802
        /*
803
         * (non-Javadoc)
804
         * @see org.gvsig.raster.dataset.Buffer#free()
805
         */
806
        public void free(){
807
                cache.getAccessPage().free();
808
                for (int i = 0; i < cache.getNPags(); i++) {
809
                        if(cache.getPageBufferFromNumberCachePage(i) != null)
810
                                cache.getPageBufferFromNumberCachePage(i).free();
811
                }
812
                try {
813
                        clearCache();
814
                } catch (IOException e) {
815
                }
816
        }
817
        
818
        //****************************************************
819
        //*********Implementing Disposable methods************
820
        //****************************************************
821
        
822
        /*
823
         * (non-Javadoc)
824
         * @see org.gvsig.tools.dispose.Disposable#dispose()
825
         */
826
        public void dispose() {
827
                free();
828
        }
829
}