Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / cache / RasterCache.java @ 11453

History | View | Annotate | Download (24.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19

    
20
package org.gvsig.raster.buffer.cache;
21

    
22
import java.io.File;
23
import java.io.FileNotFoundException;
24
import java.io.IOException;
25

    
26
import org.gvsig.raster.buffer.IBand;
27
import org.gvsig.raster.buffer.RasterBand;
28
import org.gvsig.raster.buffer.RasterBuffer;
29
import org.gvsig.raster.dataset.IBuffer;
30
import org.gvsig.raster.util.RasterUtilities;
31

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

    
49
        private Cache                         cache = null;
50
        private LRUAlgorithm          lru = null;
51
                
52
        //TODO: FUNCIONALIDAD: Intercambio de bandas para el buffer cacheado
53
        
54
         public class CacheBand extends RasterBand{
55
                 public ICacheDataSource[] cacheDataServer = null;
56
                 
57
                 public CacheBand(int height, int width){
58
                         super(height, width);
59
                 }
60
                 
61
                 public Object getLine(int line) {
62
                         return null;
63
                 }
64
                 
65
                 public void setLine(int line, Object value){
66
                        
67
                 }
68
                 
69
                 public Object getBuf(){
70
                         return null;
71
                 }
72
                 
73
                 public void setFileName(int numBand){
74
                        for (int i = 0; i < cacheDataServer.length; i++) 
75
                                ((CacheDataServer)cacheDataServer[i]).setName(null, numBand, i);
76
                 }
77
        }
78
         
79
    /**
80
     * Constructor. Asigna las variables de inicializaci?n y crea la estructura de 
81
     * la cach? con los datos pasados.
82
     * @param dataType Tipo de dato del buffer
83
     * @param width Ancho del buffer
84
     * @param height Alto del buffer
85
     * @param nBands N?mero de bandas del buffer
86
     */
87
        public RasterCache(int dataType, int width, int height, int nBands){
88
                cache = new Cache(nBands, dataType, width, height);
89
                lru = new LRUAlgorithm(cache);
90
                
91
            this.dataType = dataType;
92
        this.width = width;
93
        this.height = height;
94
        this.nBands = nBands;
95
        }
96
        
97
        /*
98
     * (non-Javadoc)
99
     * @see org.gvsig.raster.driver.IBuffer#isBandSwitchable()
100
     */
101
    public boolean isBandSwitchable(){
102
            return true;
103
    }
104
    
105
        public void malloc(int dataType, int width, int height, int bandNr) {
106
        }
107

    
108
        /**
109
         * Obtiene la cach?
110
         * @return
111
         */
112
        public Cache getCache() {
113
                return cache;
114
        }
115
        
116
        /**
117
         * Asigna la cache
118
         * @param cache
119
         */
120
        public void setCache(Cache cache) {
121
                this.cache = cache;
122
                this.lru.setCache(cache);
123
        }
124
        
125
        //*********************************************************
126
        
127
        public byte[][] getLineByte(int line) {
128
                try {
129
                        lru.cacheAccess(line, true);
130
                } catch (InvalidPageNumberException e) {return null;}
131
                return cache.getAccessPage().getLineByte((line & cache.getOffset()));
132
        }
133

    
134
        public short[][] getLineShort(int line) {
135
                try {
136
                        lru.cacheAccess(line, true);
137
                } catch (InvalidPageNumberException e) {return null;}
138
                return cache.getAccessPage().getLineShort((line & cache.getOffset()));
139
        }
140

    
141
        public int[][] getLineInt(int line) {
142
                try {
143
                        lru.cacheAccess(line, true);
144
                } catch (InvalidPageNumberException e) {return null;}
145
                return cache.getAccessPage().getLineInt((line & cache.getOffset()));
146
        }
147

    
148
        public float[][] getLineFloat(int line) {
149
                try {
150
                        lru.cacheAccess(line, true);
151
                } catch (InvalidPageNumberException e) {return null;}
152
                return cache.getAccessPage().getLineFloat((line & cache.getOffset()));
153
        }
154

    
155
        public double[][] getLineDouble(int line) {
156
                try {
157
                        lru.cacheAccess(line, true);
158
                } catch (InvalidPageNumberException e) {return null;}
159
                return cache.getAccessPage().getLineDouble((line & cache.getOffset()));
160
        }
161

    
162
        //*********************************************************
163
        
164
        public void setLineByte(byte[][] data, int line) {
165
                try {
166
                        lru.cacheAccess(line, false);
167
                } catch (InvalidPageNumberException e) {return;}
168
                cache.getAccessPage().setLineByte(data, (line & cache.getOffset()));
169
        }
170

    
171
        public void setLineShort(short[][] data, int line) {
172
                try {
173
                        lru.cacheAccess(line, false);
174
                } catch (InvalidPageNumberException e) {return;}
175
                cache.getAccessPage().setLineShort(data, (line & cache.getOffset()));
176
        }
177

    
178
        public void setLineInt(int[][] data, int line) {
179
                try {
180
                        lru.cacheAccess(line, false);
181
                } catch (InvalidPageNumberException e) {return;}
182
                cache.getAccessPage().setLineInt(data, (line & cache.getOffset()));
183
        }
184

    
185
        public void setLineFloat(float[][] data, int line) {
186
                try {
187
                        lru.cacheAccess(line, false);
188
                } catch (InvalidPageNumberException e) {return;}
189
                cache.getAccessPage().setLineFloat(data, (line & cache.getOffset()));
190
        }
191

    
192
        public void setLineDouble(double[][] data, int line) {
193
                try {
194
                        lru.cacheAccess(line, false);
195
                } catch (InvalidPageNumberException e) {return;}
196
                cache.getAccessPage().setLineDouble(data, (line & cache.getOffset()));
197
        }
198

    
199
        //*********************************************************
200
        
201
        public byte[] getLineFromBandByte(int line, int band) {
202
                try {
203
                        lru.cacheAccess(line, true);
204
                } catch (InvalidPageNumberException e) {return null;}
205
                return cache.getAccessPage().getLineFromBandByte((line & cache.getOffset()), band);
206
        }
207

    
208
        public short[] getLineFromBandShort(int line, int band) {
209
                try {
210
                        lru.cacheAccess(line, true);
211
                } catch (InvalidPageNumberException e) {return null;}
212
                return cache.getAccessPage().getLineFromBandShort((line & cache.getOffset()), band);
213
        }
214

    
215
        public int[] getLineFromBandInt(int line, int band) {
216
                try {
217
                        lru.cacheAccess(line, true);
218
                } catch (InvalidPageNumberException e) {return null;}
219
                return cache.getAccessPage().getLineFromBandInt((line & cache.getOffset()), band);
220
        }
221

    
222
        public float[] getLineFromBandFloat(int line, int band) {
223
                try {
224
                        lru.cacheAccess(line, true);
225
                } catch (InvalidPageNumberException e) {return null;}
226
                return cache.getAccessPage().getLineFromBandFloat((line & cache.getOffset()), band);
227
        }
228

    
229
        public double[] getLineFromBandDouble(int line, int band) {
230
                try {
231
                        lru.cacheAccess(line, true);
232
                } catch (InvalidPageNumberException e) {return null;}
233
                return cache.getAccessPage().getLineFromBandDouble((line & cache.getOffset()), band);
234
        }
235

    
236
        //*********************************************************
237
        
238
        public void setLineInBandByte(byte[] data, int line, int band) {
239
                try {
240
                        lru.cacheAccess(line, false);
241
                } catch (InvalidPageNumberException e) {return;}
242
                cache.getAccessPage().setLineInBandByte(data, (line & cache.getOffset()), band);
243
        }
244

    
245
        public void setLineInBandShort(short[] data, int line, int band) {
246
                try {
247
                        lru.cacheAccess(line, false);
248
                } catch (InvalidPageNumberException e) {return;}
249
                cache.getAccessPage().setLineInBandShort(data, (line & cache.getOffset()), band);
250
        }
251

    
252
        public void setLineInBandInt(int[] data, int line, int band) {
253
                try {
254
                        lru.cacheAccess(line, false);
255
                } catch (InvalidPageNumberException e) {return;}
256
                cache.getAccessPage().setLineInBandInt(data, (line & cache.getOffset()), band);
257
        }
258

    
259
        public void setLineInBandFloat(float[] data, int line, int band) {
260
                try {
261
                        lru.cacheAccess(line, false);
262
                } catch (InvalidPageNumberException e) {return;}
263
                cache.getAccessPage().setLineInBandFloat(data, (line & cache.getOffset()), band);
264
        }
265

    
266
        public void setLineInBandDouble(double[] data, int line, int band) {
267
                try {
268
                        lru.cacheAccess(line, false);
269
                } catch (InvalidPageNumberException e) {return;}
270
                cache.getAccessPage().setLineInBandDouble(data, (line & cache.getOffset()), band);
271
        }
272

    
273
        //*********************************************************
274
        
275
        public byte getElemByte(int line, int col, int band) {
276
                try {
277
                        lru.cacheAccess(line, true);
278
                } catch (InvalidPageNumberException e) {
279
                        return (byte)getNoDataValue(); //No leemos el dato
280
                }
281
                return cache.getAccessPage().getElemByte((line & cache.getOffset()), col, band);
282
        }
283

    
284
        public short getElemShort(int line, int col, int band) {
285
                try {
286
                        lru.cacheAccess(line, true);
287
                } catch (InvalidPageNumberException e) {
288
                        return (short)getNoDataValue(); //No leemos el dato
289
                }
290
                return cache.getAccessPage().getElemShort((line & cache.getOffset()), col, band);
291
        }
292

    
293
        public int getElemInt(int line, int col, int band) {
294
                try {
295
                        lru.cacheAccess(line, true);
296
                } catch (InvalidPageNumberException e) {
297
                        return (int)getNoDataValue(); //No leemos el dato
298
                }
299
                return cache.getAccessPage().getElemInt((line & cache.getOffset()), col, band);
300
        }
301

    
302
        public float getElemFloat(int line, int col, int band) {
303
                try {
304
                        lru.cacheAccess(line, true);
305
                } catch (InvalidPageNumberException e) {
306
                        return (float)getNoDataValue(); //No leemos el dato
307
                }
308
                return cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, band);
309
        }
310

    
311
        public double getElemDouble(int line, int col, int band) {
312
                try {
313
                        lru.cacheAccess(line, true);
314
                } catch (InvalidPageNumberException e) {
315
                        return (double)getNoDataValue(); //No leemos el dato
316
                }
317
                return cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, band);
318
        }
319

    
320
        //*********************************************************
321
        
322
        public void setElem(int line, int col, int band, byte data) {
323
                try {
324
                        lru.cacheAccess(line, false);
325
                } catch (InvalidPageNumberException e) { return;}
326
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
327
        }
328

    
329
        public void setElem(int line, int col, int band, short data) {
330
                try {
331
                        lru.cacheAccess(line, false);
332
                } catch (InvalidPageNumberException e) {return;}
333
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
334
        }
335

    
336
        public void setElem(int line, int col, int band, int data) {
337
                try {
338
                        lru.cacheAccess(line, false);
339
                } catch (InvalidPageNumberException e) {return;}
340
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
341
        }
342

    
343
        public void setElem(int line, int col, int band, float data) {
344
                try {
345
                        lru.cacheAccess(line, false);
346
                } catch (InvalidPageNumberException e) {return;}
347
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
348
        }
349

    
350
        public void setElem(int line, int col, int band, double data) {
351
                try {
352
                        lru.cacheAccess(line, false);
353
                } catch (InvalidPageNumberException e) {return;}
354
                cache.getAccessPage().setElem((line & cache.getOffset()), col, band, data);
355
        }
356
        
357
        //*********************************************************
358

    
359
        public void getElemByte(int line, int col, byte[] data) {
360
                try {
361
                        lru.cacheAccess(line, true);
362
                } catch (InvalidPageNumberException e) {
363
                        for (int iBand = 0; iBand < data.length; iBand++)
364
                    data[iBand] = (byte)getNoDataValue();
365
                }
366
                cache.getAccessPage().getElemByte((line & cache.getOffset()), col, data);
367
        }
368

    
369
        public void getElemShort(int line, int col, short[] data) {
370
                try {
371
                        lru.cacheAccess(line, true);
372
                } catch (InvalidPageNumberException e) {
373
                        for (int iBand = 0; iBand < data.length; iBand++)
374
                    data[iBand] = (short)getNoDataValue();
375
                }
376
                cache.getAccessPage().getElemShort((line & cache.getOffset()), col, data);
377
        }
378

    
379
        public void getElemInt(int line, int col, int[] data) {
380
                try {
381
                        lru.cacheAccess(line, true);
382
                } catch (InvalidPageNumberException e) {
383
                        for (int iBand = 0; iBand < data.length; iBand++)
384
                    data[iBand] = (int)getNoDataValue();
385
                }
386
                cache.getAccessPage().getElemInt((line & cache.getOffset()), col, data);
387
        }
388

    
389
        public void getElemFloat(int line, int col, float[] data) {
390
                try {
391
                        lru.cacheAccess(line, true);
392
                } catch (InvalidPageNumberException e) {
393
                        for (int iBand = 0; iBand < data.length; iBand++)
394
                    data[iBand] = (float)getNoDataValue();
395
                }
396
                cache.getAccessPage().getElemFloat((line & cache.getOffset()), col, data);
397
        }
398

    
399
        public void getElemDouble(int line, int col, double[] data) {
400
                try {
401
                        lru.cacheAccess(line, true);
402
                } catch (InvalidPageNumberException e) {
403
                        for (int iBand = 0; iBand < data.length; iBand++)
404
                    data[iBand] = (double)getNoDataValue();
405
                }
406
                cache.getAccessPage().getElemDouble((line & cache.getOffset()), col, data);
407
        }
408
        
409
        //*********************************************************
410
        
411
        public void setElemByte(int line, int col, byte[] data) {
412
                try {
413
                        lru.cacheAccess(line, false);
414
                } catch (InvalidPageNumberException e) {return;}
415
                cache.getAccessPage().setElemByte((line & cache.getOffset()), col, data);
416
        }
417

    
418
        public void setElemShort(int line, int col, short[] data) {
419
                try {
420
                        lru.cacheAccess(line, false);
421
                } catch (InvalidPageNumberException e) {return;}
422
                cache.getAccessPage().setElemShort((line & cache.getOffset()), col, data);
423
        }
424

    
425
        public void setElemInt(int line, int col, int[] data) {
426
                try {
427
                        lru.cacheAccess(line, false);
428
                } catch (InvalidPageNumberException e) {return;}
429
                cache.getAccessPage().setElemInt((line & cache.getOffset()), col, data);
430
        }
431

    
432
        public void setElemFloat(int line, int col, float[] data) {
433
                try {
434
                        lru.cacheAccess(line, false);
435
                } catch (InvalidPageNumberException e) {return;}
436
                cache.getAccessPage().setElemFloat((line & cache.getOffset()), col, data);
437
        }
438

    
439
        public void setElemDouble(int line, int col, double[] data) {
440
                try {
441
                        lru.cacheAccess(line, false);
442
                } catch (InvalidPageNumberException e) {return;}
443
                cache.getAccessPage().setElemDouble((line & cache.getOffset()), col, data);
444
        }
445

    
446
    /*
447
     *  (non-Javadoc)
448
     * @see org.gvsig.fmap.driver.IBuffer#getBandBuffer(int)
449
     */
450
    public IBuffer getBandBuffer(int iBand){
451
            RasterCache rasterCache = new RasterCache(getDataType(), getWidth(), getHeight(), 1);
452
            CacheStruct cs = new CacheStruct();
453
            cs.setHPag(cache.getCacheStruct().getHPag());
454
            cs.setOffset(cache.getCacheStruct().getOffset());
455
            cs.setNPags(cache.getCacheStruct().getNPags());
456
            cs.setNBands(1);
457
            cs.setNGroups(cache.getCacheStruct().getNGroups());
458
            cs.setBitsPag(cache.getCacheStruct().getBitsPag());
459
            cs.setNTotalPags((int)(getHeight() / cache.getCacheStruct().getHPag()));
460
            cs.setDataType(cache.getCacheStruct().getDataType());
461
            
462
            Cache c = new Cache(cs, getWidth());
463
            rasterCache.setCache(c);
464
            
465
            IBand band = getBand(iBand);
466
            rasterCache.assignBand(0, band);
467
            return rasterCache;
468
    }
469
    
470
    /*
471
     *  (non-Javadoc)
472
     * @see org.gvsig.fmap.driver.IBuffer#replicateBand(int, int)
473
     */
474
        public void replicateBand(int orig, int dest) {
475
        }
476
                
477
        /*
478
     *  (non-Javadoc)
479
     * @see org.gvsig.fmap.dataaccess.buffer.RasterBuffer#switchBands(int[])
480
     */
481
    public void switchBands(int[] bandPosition){
482
            
483
    }
484

    
485
        //*********************************************************
486
        
487
        public void assign(int band, byte value) {
488
                for(int line = 0; line < height; line ++){
489
                        boolean beginLine = true;  //Para acelerar solo comprobar? si la p?gina est? en cach? cada vez que empieza una l?nea
490
                    for(int col = 0; col < width; col ++){
491
                            try {
492
                                    if(beginLine){
493
                                            lru.cacheAccess(line, false);
494
                                            beginLine = false;
495
                                    }
496
                            } catch (InvalidPageNumberException e) {return;}
497
                            cache.getAccessPage().setElem((line & cache.getOffset()), col, band, value);                
498
                    }
499
                }
500
        }
501

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

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

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

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

    
595
    /*
596
         * (non-Javadoc)
597
         * @see org.gvsig.raster.driver.IBuffer#getBand(int)
598
         */
599
    public IBand getBand(int band) {
600
            CacheBand cb = new CacheBand(getHeight(), getWidth());
601
            cb.cacheDataServer = new CacheDataServer[cache.getNTotalPags()];
602
            
603
            try {
604
                        cache.resetCache();
605
                } catch (IOException e) {
606
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
607
                        return null;
608
                }
609
            for (int iPage = 0; iPage < cache.getNTotalPags(); iPage++)
610
                    cb.cacheDataServer[iPage] = cache.getHddPage(iPage, band);        
611
                    
612
            return cb;
613
    }
614
    
615
    /*
616
     *  (non-Javadoc)
617
     * @see org.gvsig.fmap.driver.IBuffer#copyBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
618
     */
619
        public void copyBand(int nBand, IBand band) {
620
                if(band instanceof CacheBand){
621
                        CacheBand cb = new CacheBand(band.getHeight(), band.getWidth());
622
                        cb.cacheDataServer = new CacheDataServer[((CacheBand)band).cacheDataServer.length];
623
                        
624
                        for (int iPage = 0; iPage < cb.cacheDataServer.length; iPage++) {
625
                                cb.cacheDataServer[iPage] = new CacheDataServer(null, nBand, iPage);
626
                                String path = ((CacheBand)band).cacheDataServer[iPage].getPath();
627
                                File f = new File(path);
628
                                if(f.exists()){
629
                                        try {
630
                                                RasterUtilities.copyFile(path, cb.cacheDataServer[iPage].getPath());
631
                                        } catch (FileNotFoundException e) {
632
                                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
633
                                                e.printStackTrace();
634
                                        } catch (IOException e) {
635
                                                e.printStackTrace();
636
                                        }
637
                                }
638
                        }
639
                        try {
640
                                cache.deleteBand(nBand);
641
                                cache.assignBand(nBand, cb.cacheDataServer);
642
                        } catch (IOException e) {
643
                                e.printStackTrace();
644
                        }
645
                                                
646
                }                
647
        }
648

    
649
        /*
650
         *  (non-Javadoc)
651
         * @see org.gvsig.fmap.driver.IBuffer#assignBand(int, org.gvsig.fmap.dataaccess.buffer.IBand)
652
         */
653
        public void assignBand(int nBand, IBand band) {
654
                if(band instanceof CacheBand) {
655
                        //((CacheBand)band).setFileName(nBand);
656
                        try {
657
                                cache.deleteBand(nBand);
658
                                cache.assignBand(nBand, ((CacheBand)band).cacheDataServer);
659
                        } catch (IOException e) {
660
                                //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
661
                                return;
662
                        }
663
                        
664
                }
665
        }
666

    
667
        /*
668
         *  (non-Javadoc)
669
         * @see org.gvsig.fmap.driver.IBuffer#createBand(byte)
670
         */
671
        public IBand createBand(byte defaultValue) {
672
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
673
                IBand band = null;
674
                try {
675
                        band = createBand(pageBuffer);
676
                } catch (IOException e) {
677
                        return null;
678
                }
679
                loadPage(new Byte(defaultValue), pageBuffer);
680
                return band;
681
        }
682
                
683
        /*
684
         *  (non-Javadoc)
685
         * @see org.gvsig.fmap.driver.IBuffer#assignBandToNotValid(int)
686
         */
687
        public void assignBandToNotValid(int iBand) {
688
                PageBandBuffer pageBuffer = new PageBandBuffer(getDataType(), getWidth(), cache.getHPag(), 1, true, 0);
689

    
690
                switch(getDataType()){
691
            case IBuffer.TYPE_BYTE:         loadPage(new Byte((byte)getNotValidValue()), pageBuffer);break;
692
            case IBuffer.TYPE_SHORT:        loadPage(new Short((short)getNotValidValue()), pageBuffer);break;
693
            case IBuffer.TYPE_INT:                loadPage(new Integer((int)getNotValidValue()), pageBuffer);break;
694
            case IBuffer.TYPE_FLOAT:        loadPage(new Float((float)getNotValidValue()), pageBuffer);break;
695
            case IBuffer.TYPE_DOUBLE:        loadPage(new Double((double)getNotValidValue()), pageBuffer);break;
696
            }        
697
                
698
                try {
699
                        CacheBand cb = (CacheBand)createBand(pageBuffer);
700
                        cb.setFileName(iBand);
701
                        assignBand(iBand, cb);
702
                } catch (IOException e) {
703
                        //TODO: EXCEPCIONES: Modificar el manejo de excepciones de RasterBuffer para q lance las apropiadas con cach?
704
                        e.printStackTrace();
705
                }
706
        }
707

    
708
        /**
709
         * Creaci?n de una banda a partir de una pagina de cache cargada con datos.
710
         * @param pageBuffer Pagina de cache cargada de datos
711
         * @param numBand N?mero de banda a la que representa
712
         * @return IBand
713
         * @throws IOException
714
         */
715
        private IBand createBand(PageBandBuffer pageBuffer) throws IOException{
716
                CacheDataServer[] ds = new CacheDataServer[cache.getNTotalPags()];
717
                for (int i = 0; i < cache.getNTotalPags(); i++) {
718
                        ds[i] = new CacheDataServer(null, 0, i);
719
                        ds[i].savePage(pageBuffer);
720
                }
721
                CacheBand band = new CacheBand(getHeight(), getWidth());
722
                band.cacheDataServer = ds;
723
                return band;
724
        }
725
        
726
        /**
727
         * Carga la p?gina con el valor pasado por par?metro. El valor ser? del mismo tipo de dato 
728
         * que el buffer actual.
729
         * @param value Valor a inicializar
730
         * @param pageBuffer P?gina
731
         */
732
        private void loadPage(Object value, PageBandBuffer pageBuffer){
733
                switch(getDataType()){
734
            case IBuffer.TYPE_BYTE: for(int i = 0 ; i < getWidth(); i ++)
735
                                                                    for(int j = 0 ; j < cache.getHPag(); j ++)
736
                                                                            pageBuffer.setElem(j, i, 0, ((Byte)value).byteValue());
737
                                                            break;
738
            case IBuffer.TYPE_SHORT: for(int i = 0 ; i < getWidth(); i ++)
739
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
740
                                                                                pageBuffer.setElem(j, i, 0, ((Short)value).shortValue());
741
                                                                 break;
742
            case IBuffer.TYPE_INT:        for(int i = 0 ; i < getWidth(); i ++)
743
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
744
                                                                                pageBuffer.setElem(j, i, 0, ((Integer)value).intValue());
745
                                                                break;
746
            case IBuffer.TYPE_FLOAT:for(int i = 0 ; i < getWidth(); i ++)
747
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
748
                                                                                pageBuffer.setElem(j, i, 0, ((Float)value).floatValue());
749
                                                                        break;
750
            case IBuffer.TYPE_DOUBLE:for(int i = 0 ; i < getWidth(); i ++)
751
                                                                        for(int j = 0 ; j < cache.getHPag(); j ++)
752
                                                                                pageBuffer.setElem(j, i, 0, ((Double)value).doubleValue());
753
                                                                 break;
754
            }        
755
        }
756
}