Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG-mobile / libCompatMobile / src / org / gvsig / compat / FileUtils.java @ 20386

History | View | Annotate | Download (3.9 KB)

1
package org.gvsig.compat;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.DataInputStream;
5
import java.io.EOFException;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.IOException;
9
import java.nio.ByteBuffer;
10
import java.nio.channels.FileChannel;
11

    
12
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
13
 *
14
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
29
 *
30
 * For more information, contact:
31
 *
32
 *   Generalitat Valenciana
33
 *   Conselleria d'Infraestructures i Transport
34
 *   Av. Blasco Ib??ez, 50
35
 *   46010 VALENCIA
36
 *   SPAIN
37
 *
38
 *   +34 963862235
39
 *   gvsig@gva.es
40
 *   http://www.gvsig.gva.es
41
 *
42
 *    or
43
 *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL
45
 *   Conde Salvatierra de ?lava , 34-10
46
 *   46004 Valencia
47
 *   Spain
48
 *
49
 *   +34 963 510 612
50
 *   +34 963 510 968
51
 *   gis@prodevelop.es
52
 *   http://www.prodevelop.es
53
 *
54
 *    or
55
 *
56
 *   Instituto de Rob?tica
57
 *   Apartado de correos 2085
58
 *   46071 Valencia
59
 *   (Spain)
60
 *   
61
 *   +34 963 543 577
62
 *   jjordan@robotica.uv.es
63
 *   http://robotica.uv.es
64
 *   
65
 */
66
/* CVS MESSAGES:
67
 * -------------
68
 * $Id$
69
 * $Log$
70
 *
71
 */
72
/****************************************************************
73
 *                                                                                                                                
74
 * PRODEVELOP S.L.                                                                                                
75
 * @author Carlos S?nchez Peri??n (csanchez@prodevelop.es)                
76
 *                                                                                                                                
77
 ****************************************************************/
78
public class FileUtils {
79
        /******************************************************************
80
         * readFile(): Read a file and return the result in an bytes array. 
81
         * 
82
         * @return byte[]
83
         * @param File
84
         ******************************************************************/
85
    public static byte[] readFileJ2SE(File f){
86
            byte[] data = null;
87
            try{
88
                FileInputStream fis = new FileInputStream(f);
89
                FileChannel fc = fis.getChannel();
90
                data=  new byte[(int)fc.size()];   // fc.size returns the size of the file which backs the channel
91
                ByteBuffer bb = ByteBuffer.wrap(data);
92
                fc.read(bb);
93
            } catch (Exception e){}
94
        return data;
95
    }
96
    
97
        /******************************************************************
98
         * readFile(): Read a file and return the result in an bytes array. 
99
         * 
100
         * @return byte[]
101
         * @param File
102
         * @throws IOException 
103
         ******************************************************************/
104
  public static byte[] readFileJ2ME(File f) throws IOException{
105
          byte[] data = null;
106
              int numBytes=0;
107
                        
108
     FileInputStream fis = new FileInputStream(f);
109
                    //set the FilterInputStream to select the type of data to read
110
                    DataInputStream dis = new DataInputStream(fis);
111
                    BufferedInputStream bis = new BufferedInputStream(fis);
112
                       try{
113
                        while(true){
114
                            dis.read();
115
                            numBytes++;
116
                }
117
                       } catch (EOFException e){
118
                        //Final de fichero
119
                        }
120
                    data=new byte[numBytes];
121
                    dis.read(data);
122
                    dis.close();
123
                    bis.close();
124
                    fis.close();
125
                    return data;
126
  }
127
}