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

History | View | Annotate | Download (24.4 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
                 public void dispose() {
85
                         for (int i = 0; i < cacheDataServer.length; i++) {
86
                                 cacheDataServer[i] = null;
87
                         }
88
                         cacheDataServer = null;
89
                 }
90
        }
91
         
92
    /**
93
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
94
     * la cach? con los datos pasados.
95
     * @param dataType Tipo de dato del buffer
96
     * @param width Ancho del buffer
97
     * @param height Alto del buffer
98
     * @param nBands N?mero de bandas del buffer
99
     */
100
        public RasterCache(int dataType, int width, int height, int nBands){
101
                cache = new Cache(nBands, dataType, width, height);
102
                lru = new LRUAlgorithm(cache);
103
                
104
            this.dataType = dataType;
105
        this.width = width;
106
        this.height = height;
107
        this.nBands = nBands;
108
        }
109
        
110
        /**
111
         * Limpia los trozos de cach? en disco. Despu?s del llamar a este 
112
         * m?todo no puede volver a usarse esta cach?.
113
         * @throws IOException 
114
         */
115
        public void clearCache() throws IOException {
116
                cache.clearCache(this.nBands);
117
        }
118
        
119
    public boolean isBandSwitchable(){
120
            return true;
121
    }
122
    
123
        public void malloc(int dataType, int width, int height, int bandNr) {
124
        }
125

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

    
153
        public short[][] getLineShort(int line) {
154
        if (exists(line)) {
155
            return cache.getAccessPage().getLineShort(
156
                (line & cache.getOffset()));
157
        }
158
        return null;
159
        }
160

    
161
        public int[][] getLineInt(int line) {
162
        if (exists(line)) {
163
            return cache.getAccessPage().getLineInt((line & cache.getOffset()));
164
        }
165
        return null;
166
        }
167

    
168
        public float[][] getLineFloat(int line) {
169
        if (exists(line)) {
170
            return cache.getAccessPage().getLineFloat(
171
                (line & cache.getOffset()));
172
        }
173
        return null;
174
        }
175

    
176
        public double[][] getLineDouble(int line) {
177
        if (exists(line)) {
178
            return cache.getAccessPage().getLineDouble(
179
                (line & cache.getOffset()));
180
        }
181
        return null;
182
        }
183

    
184
        //*********************************************************
185
        
186
        public void setLineByte(byte[][] data, int line) {
187
        if (exists(line)) {
188
            cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
189
        }
190
        }
191

    
192
        public void setLineShort(short[][] data, int line) {
193
        if (exists(line)) {
194
            cache.getAccessPage()
195
                .setLineShort(data, (line & cache.getOffset()));
196
        }
197
        }
198

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

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

    
212
        public void setLineDouble(double[][] data, int line) {
213
        if (exists(line)) {
214
            cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
215
        }
216
        }
217

    
218
        //*********************************************************
219
        
220
        public byte[] getLineFromBandByte(int line, int band) {
221
        if (exists(line)) {
222
            return cache.getAccessPage().getLineFromBandByte(
223
                (line & cache.getOffset()), band);
224
        }
225
        return null;
226
        }
227

    
228
        public short[] getLineFromBandShort(int line, int band) {
229
        if (exists(line)) {
230
            return cache.getAccessPage().getLineFromBandShort(
231
                (line & cache.getOffset()), band);
232
        }
233
        return null;
234
        }
235

    
236
        public int[] getLineFromBandInt(int line, int band) {
237
        if (exists(line)) {
238
            return cache.getAccessPage().getLineFromBandInt(
239
                (line & cache.getOffset()), band);
240
        }
241
        return null;
242
        }
243

    
244
        public float[] getLineFromBandFloat(int line, int band) {
245
        if (exists(line)) {
246
            return cache.getAccessPage().getLineFromBandFloat(
247
                (line & cache.getOffset()), band);
248
        }
249
        return null;
250
        }
251

    
252
        public double[] getLineFromBandDouble(int line, int band) {
253
        if (exists(line)) {
254
            return cache.getAccessPage().getLineFromBandDouble(
255
                (line & cache.getOffset()), band);
256
        }
257
        return null;
258
        }
259

    
260
        //*********************************************************
261
        
262
        public void setLineInBandByte(byte[] data, int line, int band) {
263
        if (exists(line)) {
264
            cache.getAccessPage().setLineInBandByte(data,
265
                (line & cache.getOffset()), band);
266
        }
267
        }
268

    
269
        public void setLineInBandShort(short[] data, int line, int band) {
270
        if (exists(line)) {
271
            cache.getAccessPage().setLineInBandShort(data,
272
                (line & cache.getOffset()), band);
273
        }
274
        }
275

    
276
        public void setLineInBandInt(int[] data, int line, int band) {
277
        if (exists(line)) {
278
            cache.getAccessPage().setLineInBandInt(data,
279
                (line & cache.getOffset()), band);
280
        }
281
        }
282

    
283
        public void setLineInBandFloat(float[] data, int line, int band) {
284
        if (exists(line)) {
285
            cache.getAccessPage().setLineInBandFloat(data,
286
                (line & cache.getOffset()), band);
287
        }
288
        }
289

    
290
        public void setLineInBandDouble(double[] data, int line, int band) {
291
        if (exists(line)) {
292
            cache.getAccessPage().setLineInBandDouble(data,
293
                (line & cache.getOffset()), band);
294
        }
295
        }
296

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

    
307
        public short getElemShort(int line, int col, int band) {
308
        if (exists(line)) {
309
            return cache.getAccessPage().getElemShort(
310
                (line & cache.getOffset()), col, band);
311
                }
312
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().shortValue() : RasterLibrary.defaultShortNoDataValue; // No leemos el dato
313
        }
314

    
315
        public int getElemInt(int line, int col, int band) {
316
        if (exists(line)) {
317
            return cache.getAccessPage().getElemInt((line & cache.getOffset()),
318
                col, band);
319
                }
320
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().intValue() : RasterLibrary.defaultIntegerNoDataValue; // No leemos el dato
321
        }
322

    
323
        public float getElemFloat(int line, int col, int band) {
324
        if (exists(line)) {
325
            return cache.getAccessPage().getElemFloat(
326
                (line & cache.getOffset()), col, band);
327
                }
328
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().floatValue() : RasterLibrary.defaultFloatNoDataValue; // No leemos el dato
329
        }
330

    
331
        public double getElemDouble(int line, int col, int band) {
332
        if (exists(line)) {
333
            return cache.getAccessPage().getElemDouble(
334
                (line & cache.getOffset()), col, band);
335
                }
336
        return getNoDataValue().isDefined() ? getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue; // No leemos el dato
337
        }
338

    
339
        //*********************************************************
340
        
341
        public void setElem(int line, int col, int band, byte data) {
342
        if (exists(line)) {
343
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
344
                band, data);
345
        }
346
        }
347

    
348
        public void setElem(int line, int col, int band, short data) {
349
        if (exists(line)) {
350
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
351
                band, data);
352
        }
353
        }
354

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

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

    
369
        public void setElem(int line, int col, int band, double data) {
370
        if (exists(line)) {
371
            cache.getAccessPage().setElem((line & cache.getOffset()), col,
372
                band, data);
373
        }
374
        }
375
        
376
        //*********************************************************
377

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

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

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

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

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

    
434
    public void setElemByte(int line, int col, byte[] data) {
435
        if (exists(line)) {
436
            cache.getAccessPage().setElemByte((line & cache.getOffset()), col,
437
                data);
438
        }
439
    }
440

    
441
    public void setElemShort(int line, int col, short[] data) {
442
        if (exists(line)) {
443
            cache.getAccessPage().setElemShort((line & cache.getOffset()), col,
444
                data);
445
        }
446
    }
447

    
448
    public void setElemInt(int line, int col, int[] data) {
449
        if (exists(line)) {
450
            cache.getAccessPage().setElemInt((line & cache.getOffset()), col,
451
                data);
452
        }
453
    }
454

    
455
    public void setElemFloat(int line, int col, float[] data) {
456
        if (exists(line)) {
457
            cache.getAccessPage().setElemFloat((line & cache.getOffset()), col,
458
                data);
459
        }
460
    }
461

    
462
    public void setElemDouble(int line, int col, double[] data) {
463
        if (exists(line)) {
464
            cache.getAccessPage().setElemDouble((line & cache.getOffset()),
465
                col, data);
466
        }
467
    }
468

    
469
    public Buffer getBandBuffer(int Band){
470
            RasterCache rasterCache = new RasterCache(getDataType(), getWidth(), getHeight(), 1);
471
            CacheStruct cs = new CacheStruct();
472
            cs.setHPag(cache.getCacheStruct().getHPag());
473
            cs.setOffset(cache.getCacheStruct().getOffset());
474
            cs.setNPags(cache.getCacheStruct().getNPags());
475
            cs.setNBands(1);
476
            cs.setNGroups(cache.getCacheStruct().getNGroups());
477
            cs.setBitsPag(cache.getCacheStruct().getBitsPag());
478
            cs.setNTotalPags((int)(getHeight() / cache.getCacheStruct().getHPag()));
479
            cs.setDataType(cache.getCacheStruct().getDataType());
480
            
481
            Cache c = new Cache(cs, getWidth());
482
            rasterCache.setCache(c);
483
            
484
            Band band = getBand(Band);
485
            rasterCache.assignBand(0, band);
486
            return rasterCache;
487
    }
488
    
489
        public void replicateBand(int orig, int dest) {
490
        }
491
                
492
    public void switchBands(int[] bandPosition){
493
            
494
    }
495

    
496
        //*********************************************************
497
        
498
        public void assign(int band, byte value) {
499
                for(int line = 0; line < height; line ++){
500
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
501
                    for(int col = 0; col < width; col ++){
502
                            try {
503
                                    if(beginLine){
504
                                            lru.cacheAccess(line, false);
505
                                            beginLine = false;
506
                                    }
507
                            } catch (InvalidPageNumberException e) {return;}
508
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
509
                    }
510
                }
511
        }
512

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

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

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

    
558
        public void assign(int band, double value) {
559
                for(int line = 0; line < height; line ++){
560
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
561
                    for(int col = 0; col < width; col ++){
562
                            try {
563
                                    if(beginLine){
564
                                            lru.cacheAccess(line, false);
565
                                            beginLine = false;
566
                                    }
567
                            } catch (InvalidPageNumberException e) {return;}
568
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
569
                    }
570
                }
571
        }
572
    
573
    public Buffer cloneBuffer(){
574
            return null;
575
    }
576
    
577
        public void interchangeBands(int band1, int band2) {
578
                Band b1 = getBand(band1);
579
                Band b2 = getBand(band2);
580
                
581
                try {
582
                        cache.assignBand(band2, ((CacheBand)b1).cacheDataServer);
583
                        cache.assignBand(band1, ((CacheBand)b2).cacheDataServer);
584
                } catch (IOException e) {
585
                        e.printStackTrace();
586
                }
587
                
588
        }
589
            
590
    public void mallocOneBand(int dataType, int width, int height, int band) {
591
                        
592
        }
593

    
594
    public Band getBand(int band) {
595
            CacheBand cb = new CacheBand(getHeight(), getWidth());
596
            cb.cacheDataServer = new CacheDataServer[cache.getNTotalPags()];
597
            
598
            try {
599
                        cache.resetCache();
600
                } catch (IOException e) {
601
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
602
                        return null;
603
                }
604
            for (int iPage = 0; iPage < cache.getNTotalPags(); iPage++)
605
                    cb.cacheDataServer[iPage] = cache.getHddPage(iPage, band);        
606
                    
607
            return cb;
608
    }
609
    
610
        public void copyBand(int nBand, Band band) {
611
                FileUtils file = RasterLocator.getManager().getFileUtils();
612
                if(band instanceof CacheBand){
613
                        CacheBand cb = new CacheBand(band.getHeight(), band.getWidth());
614
                        cb.cacheDataServer = new CacheDataServer[((CacheBand)band).cacheDataServer.length];
615
                        
616
                        for (int iPage = 0; iPage < cb.cacheDataServer.length; iPage++) {
617
                                cb.cacheDataServer[iPage] = new CacheDataServer(null, nBand, iPage);
618
                                String path = ((CacheBand)band).cacheDataServer[iPage].getPath();
619
                                File f = new File(path);
620
                                if(f.exists()){
621
                                        try {
622
                                                file.copyFile(path, cb.cacheDataServer[iPage].getPath());
623
                                        } catch (FileNotFoundException e) {
624
                                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
625
                                                e.printStackTrace();
626
                                        } catch (IOException e) {
627
                                                e.printStackTrace();
628
                                        }
629
                                }
630
                        }
631
                        try {
632
                                cache.deleteBand(nBand);
633
                                cache.assignBand(nBand, cb.cacheDataServer);
634
                        } catch (IOException e) {
635
                                e.printStackTrace();
636
                        }
637
                                                
638
                }                
639
        }
640

    
641
        public void assignBand(int nBand, Band band) {
642
                if(band instanceof CacheBand) {
643
                        //((CacheBand)band).setFileName(nBand);
644
                        try {
645
                                cache.deleteBand(nBand);
646
                                cache.assignBand(nBand, ((CacheBand)band).cacheDataServer);
647
                        } catch (IOException e) {
648
                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
649
                                return;
650
                        }
651
                        
652
                }
653
        }
654

    
655
        public Band createBand(byte defaultValue) {
656
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
657
                Band band = null;
658
                try {
659
                        band = createBand(pageBuffer);
660
                } catch (IOException e) {
661
                        return null;
662
                }
663
                loadPage(new Byte(defaultValue), pageBuffer);
664
                return band;
665
        }
666
                
667
        public void assignBandToNotValid(int Band) {
668
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
669

    
670
                switch(getDataType()){
671
            case Buffer.TYPE_BYTE:         loadPage(new Byte((byte)getNotValidValue()), pageBuffer);break;
672
            case Buffer.TYPE_SHORT:        loadPage(new Short((short)getNotValidValue()), pageBuffer);break;
673
            case Buffer.TYPE_INT:                loadPage(new Integer((int)getNotValidValue()), pageBuffer);break;
674
            case Buffer.TYPE_FLOAT:        loadPage(new Float((float)getNotValidValue()), pageBuffer);break;
675
            case Buffer.TYPE_DOUBLE:        loadPage(new Double((double)getNotValidValue()), pageBuffer);break;
676
            }        
677
                
678
                try {
679
                        CacheBand cb = (CacheBand)createBand(pageBuffer);
680
                        cb.setFileName(Band);
681
                        assignBand(Band, cb);
682
                } catch (IOException e) {
683
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
684
                        e.printStackTrace();
685
                }
686
        }
687

    
688
        /**
689
         * Creaci?n de una banda a partir de una pagina de cache cargada con datos.
690
         * @param pageBuffer Pagina de cache cargada de datos
691
         * @param numBand N?mero de banda a la que representa
692
         * @return Band
693
         * @throws IOException
694
         */
695
        private Band createBand(PageBandBuffer pageBuffer) throws IOException{
696
                CacheDataServer[] ds = new CacheDataServer[cache.getNTotalPags()];
697
                for (int i = 0; i < cache.getNTotalPags(); i++) {
698
                        ds[i] = new CacheDataServer(null, 0, i);
699
                        ds[i].savePage(pageBuffer);
700
                }
701
                CacheBand band = new CacheBand(getHeight(), getWidth());
702
                band.cacheDataServer = ds;
703
                return band;
704
        }
705
        
706
        /**
707
         * Carga la p?gina con el valor pasado por par?metro. El valor ser? del mismo tipo de dato 
708
         * que el buffer actual.
709
         * @param value Valor a inicializar
710
         * @param pageBuffer P?gina
711
         */
712
        private void loadPage(Object value, PageBandBuffer pageBuffer){
713
                switch(getDataType()){
714
            case Buffer.TYPE_BYTE: for(int i = 0 ; i < getWidth(); i ++)
715
                                                                    for(int j = 0 ; j < cache.getHPag(); j ++)
716
                                                                            pageBuffer.setElem(j, i, 0, ((Byte)value).byteValue());
717
                                                            break;
718
            case Buffer.TYPE_SHORT: for(int i = 0 ; i < getWidth(); i ++)
719
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
720
                                                                                pageBuffer.setElem(j, i, 0, ((Short)value).shortValue());
721
                                                                 break;
722
            case Buffer.TYPE_INT:        for(int i = 0 ; i < getWidth(); i ++)
723
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
724
                                                                                pageBuffer.setElem(j, i, 0, ((Integer)value).intValue());
725
                                                                break;
726
            case Buffer.TYPE_FLOAT:for(int i = 0 ; i < getWidth(); i ++)
727
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
728
                                                                                pageBuffer.setElem(j, i, 0, ((Float)value).floatValue());
729
                                                                        break;
730
            case Buffer.TYPE_DOUBLE:for(int i = 0 ; i < getWidth(); i ++)
731
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
732
                                                                                pageBuffer.setElem(j, i, 0, ((Double)value).doubleValue());
733
                                                                 break;
734
            }        
735
        }
736
        
737
        public boolean isReadOnlyBuffer() {
738
                return false;
739
        }
740
        
741
        public boolean isCached() {
742
                return true;
743
        }
744
        
745
        public int getBlockHeight() {
746
                return cache.getHPag();
747
        }
748
        
749
        public void dispose() {
750
                if(cache != null) {
751
                        if(cache.getAccessPage() != null)
752
                                cache.getAccessPage().dispose();
753
                        for (int i = 0; i < cache.getNPags(); i++) {
754
                                if(cache.getPageBufferFromNumberCachePage(i) != null)
755
                                        cache.getPageBufferFromNumberCachePage(i).dispose();
756
                        }
757
                        try {
758
                                clearCache();
759
                        } catch (IOException e) {
760
                        }
761
                        cache = null;
762
                }
763
                lru = null;
764
        }
765
        
766
        protected void finalize() throws Throwable {
767
                cache = null;
768
                lru = null;
769
                super.finalize();
770
        }
771
}