affyfusion-109/src/affymetrix/gcos/FileIO.java

Code
Comments
Other
Rev Date Author Line
11 13 Sep 07 nicklas 1 /////////////////////////////////////////////////////////////////
11 13 Sep 07 nicklas 2 //
11 13 Sep 07 nicklas 3 // Copyright (C) 2005 Affymetrix, Inc.
11 13 Sep 07 nicklas 4 //
11 13 Sep 07 nicklas 5 // This library is free software; you can redistribute it and/or modify
11 13 Sep 07 nicklas 6 // it under the terms of the GNU Lesser General Public License as published
11 13 Sep 07 nicklas 7 // by the Free Software Foundation; either version 2.1 of the License,
11 13 Sep 07 nicklas 8 // or (at your option) any later version.
11 13 Sep 07 nicklas 9 //
11 13 Sep 07 nicklas 10 // This library is distributed in the hope that it will be useful, but
11 13 Sep 07 nicklas 11 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 13 Sep 07 nicklas 12 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 13 Sep 07 nicklas 13 // for more details.
11 13 Sep 07 nicklas 14 //
11 13 Sep 07 nicklas 15 // You should have received a copy of the GNU Lesser General Public License
11 13 Sep 07 nicklas 16 // along with this library; if not, write to the Free Software Foundation, Inc.,
11 13 Sep 07 nicklas 17 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
11 13 Sep 07 nicklas 18 //
11 13 Sep 07 nicklas 19 /////////////////////////////////////////////////////////////////
11 13 Sep 07 nicklas 20
11 13 Sep 07 nicklas 21
11 13 Sep 07 nicklas 22 package affymetrix.gcos;
11 13 Sep 07 nicklas 23
11 13 Sep 07 nicklas 24 import java.io.*;
11 13 Sep 07 nicklas 25 import java.nio.*;
14 14 Sep 07 nicklas 26
11 13 Sep 07 nicklas 27 import affymetrix.portability.*;
11 13 Sep 07 nicklas 28
11 13 Sep 07 nicklas 29 /**
11 13 Sep 07 nicklas 30  *
11 13 Sep 07 nicklas 31  * @author ljevon
11 13 Sep 07 nicklas 32  */
11 13 Sep 07 nicklas 33 public class FileIO {
11 13 Sep 07 nicklas 34     
11 13 Sep 07 nicklas 35     /** Creates a new instance of FileIO */
11 13 Sep 07 nicklas 36     public FileIO() {
11 13 Sep 07 nicklas 37     }
11 13 Sep 07 nicklas 38
11 13 Sep 07 nicklas 39     /** Reads an integer from a little endian file.
11 13 Sep 07 nicklas 40      * @param fis The file stream.
11 13 Sep 07 nicklas 41      * @return The integer.
11 13 Sep 07 nicklas 42      */
13 14 Sep 07 nicklas 43     public static int ReadInt32_I(InputStream fis)
11 13 Sep 07 nicklas 44     {
11 13 Sep 07 nicklas 45         byte[] b = new byte[DataSizes.INT_SIZE];
11 13 Sep 07 nicklas 46         try
11 13 Sep 07 nicklas 47         {
11 13 Sep 07 nicklas 48             fis.read(b);
11 13 Sep 07 nicklas 49             return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
11 13 Sep 07 nicklas 50         }
11 13 Sep 07 nicklas 51         catch (Throwable t)
11 13 Sep 07 nicklas 52         {
11 13 Sep 07 nicklas 53             return 0;
11 13 Sep 07 nicklas 54         }
11 13 Sep 07 nicklas 55     }
11 13 Sep 07 nicklas 56
13 14 Sep 07 nicklas 57     /** Reads an integer from a little endian file and then pushes it back again.
13 14 Sep 07 nicklas 58      * @param fis The file stream.
13 14 Sep 07 nicklas 59      * @return The integer.
13 14 Sep 07 nicklas 60      */
13 14 Sep 07 nicklas 61     public static int PeekInt32_I(PushbackInputStream fis)
13 14 Sep 07 nicklas 62     {
13 14 Sep 07 nicklas 63         byte[] b = new byte[DataSizes.INT_SIZE];
13 14 Sep 07 nicklas 64         try
13 14 Sep 07 nicklas 65         {
13 14 Sep 07 nicklas 66             fis.read(b);
13 14 Sep 07 nicklas 67             fis.unread(b);
13 14 Sep 07 nicklas 68             return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
13 14 Sep 07 nicklas 69         }
13 14 Sep 07 nicklas 70         catch (Throwable t)
13 14 Sep 07 nicklas 71         {
13 14 Sep 07 nicklas 72             return 0;
13 14 Sep 07 nicklas 73         }
13 14 Sep 07 nicklas 74     }
13 14 Sep 07 nicklas 75
13 14 Sep 07 nicklas 76     
11 13 Sep 07 nicklas 77     /** Reads a float from a little endian file.
11 13 Sep 07 nicklas 78      * @param fis The file stream.
11 13 Sep 07 nicklas 79      * @return The float.
11 13 Sep 07 nicklas 80      */
13 14 Sep 07 nicklas 81     public static float ReadFloat_I(InputStream fis)
11 13 Sep 07 nicklas 82     {
11 13 Sep 07 nicklas 83         byte[] b = new byte[DataSizes.FLOAT_SIZE];
11 13 Sep 07 nicklas 84         try
11 13 Sep 07 nicklas 85         {
11 13 Sep 07 nicklas 86             fis.read(b);
11 13 Sep 07 nicklas 87             return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getFloat();
11 13 Sep 07 nicklas 88         }
11 13 Sep 07 nicklas 89         catch (Throwable t)
11 13 Sep 07 nicklas 90         {
11 13 Sep 07 nicklas 91             return 0.0f;
11 13 Sep 07 nicklas 92         }
11 13 Sep 07 nicklas 93     }
11 13 Sep 07 nicklas 94
11 13 Sep 07 nicklas 95     /** Reads a short from a little endian file.
11 13 Sep 07 nicklas 96      * @param fis The file stream.
11 13 Sep 07 nicklas 97      * @return The short.
11 13 Sep 07 nicklas 98      */
13 14 Sep 07 nicklas 99     public static short ReadInt16_I(InputStream fis)
11 13 Sep 07 nicklas 100     {
11 13 Sep 07 nicklas 101         byte[] b = new byte[DataSizes.SHORT_SIZE];
11 13 Sep 07 nicklas 102         try
11 13 Sep 07 nicklas 103         {
11 13 Sep 07 nicklas 104             fis.read(b);
11 13 Sep 07 nicklas 105             return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
11 13 Sep 07 nicklas 106         }
11 13 Sep 07 nicklas 107         catch (Throwable t)
11 13 Sep 07 nicklas 108         {
11 13 Sep 07 nicklas 109             return 0;
11 13 Sep 07 nicklas 110         }
11 13 Sep 07 nicklas 111     }
11 13 Sep 07 nicklas 112
11 13 Sep 07 nicklas 113     /** Reads an unsigned short from a little endian file.
11 13 Sep 07 nicklas 114      * @param fis The file stream.
11 13 Sep 07 nicklas 115      * @return The value as an integer.
11 13 Sep 07 nicklas 116      */
13 14 Sep 07 nicklas 117     public static int ReadUInt16_I(InputStream fis)
11 13 Sep 07 nicklas 118     {
11 13 Sep 07 nicklas 119         byte[] b = new byte[DataSizes.SHORT_SIZE];
11 13 Sep 07 nicklas 120         try
11 13 Sep 07 nicklas 121         {
11 13 Sep 07 nicklas 122             fis.read(b);
11 13 Sep 07 nicklas 123             return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
11 13 Sep 07 nicklas 124         }
11 13 Sep 07 nicklas 125         catch (Throwable t)
11 13 Sep 07 nicklas 126         {
11 13 Sep 07 nicklas 127             return 0;
11 13 Sep 07 nicklas 128         }
11 13 Sep 07 nicklas 129     }
11 13 Sep 07 nicklas 130             
11 13 Sep 07 nicklas 131     /** Reads a byte from a little endian file.
11 13 Sep 07 nicklas 132      * @param fis The file stream.
11 13 Sep 07 nicklas 133      * @return The byte.
11 13 Sep 07 nicklas 134      */
15 17 Sep 07 nicklas 135     public static byte ReadInt8(InputStream fis)
11 13 Sep 07 nicklas 136     {
11 13 Sep 07 nicklas 137         try
11 13 Sep 07 nicklas 138         {
11 13 Sep 07 nicklas 139             return (byte) fis.read();
11 13 Sep 07 nicklas 140         }
11 13 Sep 07 nicklas 141         catch (Throwable t)
11 13 Sep 07 nicklas 142         {
11 13 Sep 07 nicklas 143             return 0;
11 13 Sep 07 nicklas 144         }
11 13 Sep 07 nicklas 145     }
11 13 Sep 07 nicklas 146             
15 17 Sep 07 nicklas 147     /** Reads a byte from a little endian file and then pushes it back again.
15 17 Sep 07 nicklas 148      * @param fis The file stream.
15 17 Sep 07 nicklas 149      * @return The byte.
15 17 Sep 07 nicklas 150      */
15 17 Sep 07 nicklas 151     public static byte PeekInt8(PushbackInputStream pin)
15 17 Sep 07 nicklas 152     {
15 17 Sep 07 nicklas 153         try
15 17 Sep 07 nicklas 154         {
15 17 Sep 07 nicklas 155             int b = pin.read();
15 17 Sep 07 nicklas 156             pin.unread(b);
15 17 Sep 07 nicklas 157             return (byte)b;
15 17 Sep 07 nicklas 158         }
15 17 Sep 07 nicklas 159         catch (Throwable t)
15 17 Sep 07 nicklas 160         {
15 17 Sep 07 nicklas 161             return 0;
15 17 Sep 07 nicklas 162         }
15 17 Sep 07 nicklas 163     }
11 13 Sep 07 nicklas 164     /** Reads a string from a little endian file.
11 13 Sep 07 nicklas 165      * @param fis The file stream.
11 13 Sep 07 nicklas 166      * @return The string.
11 13 Sep 07 nicklas 167      */
13 14 Sep 07 nicklas 168     public static String ReadString_I(InputStream fis)
11 13 Sep 07 nicklas 169     {
11 13 Sep 07 nicklas 170         try
11 13 Sep 07 nicklas 171         {
11 13 Sep 07 nicklas 172             int n = ReadInt32_I(fis);
11 13 Sep 07 nicklas 173             if (n > 0)
11 13 Sep 07 nicklas 174             {
11 13 Sep 07 nicklas 175                 return ReadFixedString(fis, n);
11 13 Sep 07 nicklas 176             }
11 13 Sep 07 nicklas 177         }
11 13 Sep 07 nicklas 178         catch (Throwable t)
11 13 Sep 07 nicklas 179         {
11 13 Sep 07 nicklas 180         }
11 13 Sep 07 nicklas 181         return null;
11 13 Sep 07 nicklas 182     }
11 13 Sep 07 nicklas 183     
11 13 Sep 07 nicklas 184     /** Read a string of fixed length.
11 13 Sep 07 nicklas 185      * @param fis The file stream.
11 13 Sep 07 nicklas 186      * @param len The length of the string.
11 13 Sep 07 nicklas 187      * @return The string.
11 13 Sep 07 nicklas 188      */
13 14 Sep 07 nicklas 189     public static String ReadFixedString(InputStream fis, int len) {
11 13 Sep 07 nicklas 190         try
11 13 Sep 07 nicklas 191         {
11 13 Sep 07 nicklas 192             byte[] b = new byte[len];
18 15 Nov 07 nicklas 193             int offset = 0;
18 15 Nov 07 nicklas 194             int numRead = 0;
18 15 Nov 07 nicklas 195             while (numRead < len)
18 15 Nov 07 nicklas 196             {
18 15 Nov 07 nicklas 197                 numRead += fis.read(b, numRead, len-numRead);
18 15 Nov 07 nicklas 198             }
11 13 Sep 07 nicklas 199             String str = new String();
11 13 Sep 07 nicklas 200             for (int i=0; i<len; i++)
11 13 Sep 07 nicklas 201             {
11 13 Sep 07 nicklas 202                 if (b[i] == 0)
11 13 Sep 07 nicklas 203                     break;
11 13 Sep 07 nicklas 204                 str += (char) b[i];
11 13 Sep 07 nicklas 205             }
11 13 Sep 07 nicklas 206             return str; 
11 13 Sep 07 nicklas 207         }
11 13 Sep 07 nicklas 208         catch (Throwable t)
11 13 Sep 07 nicklas 209         {
11 13 Sep 07 nicklas 210         }
11 13 Sep 07 nicklas 211         return null;
11 13 Sep 07 nicklas 212     }
11 13 Sep 07 nicklas 213     
11 13 Sep 07 nicklas 214     /** Reads the next line of text.
11 13 Sep 07 nicklas 215      * @param b The buffered reader.
11 13 Sep 07 nicklas 216      * @return The next line from the file or null if not found.
11 13 Sep 07 nicklas 217      */
11 13 Sep 07 nicklas 218     public static String ReadNextLine(BufferedReader b) {
11 13 Sep 07 nicklas 219         String str;
11 13 Sep 07 nicklas 220         try
11 13 Sep 07 nicklas 221         {
11 13 Sep 07 nicklas 222             while ((str = b.readLine()) != null)
11 13 Sep 07 nicklas 223             {
11 13 Sep 07 nicklas 224                 if (str.length() > 0)
11 13 Sep 07 nicklas 225                     return str;
11 13 Sep 07 nicklas 226             }
11 13 Sep 07 nicklas 227         }
11 13 Sep 07 nicklas 228         catch (Throwable t)
11 13 Sep 07 nicklas 229         {
11 13 Sep 07 nicklas 230         }
11 13 Sep 07 nicklas 231         return null;
11 13 Sep 07 nicklas 232     }
11 13 Sep 07 nicklas 233     
11 13 Sep 07 nicklas 234     /** Reads the next floating point value from the buffer.
11 13 Sep 07 nicklas 235      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 236      * @param offset The offset to the file.
11 13 Sep 07 nicklas 237      * @return The next floating point value.
11 13 Sep 07 nicklas 238      */
11 13 Sep 07 nicklas 239     public static float MmGetFloat_I(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 240         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 241         return b.getFloat();
11 13 Sep 07 nicklas 242     }
11 13 Sep 07 nicklas 243     
11 13 Sep 07 nicklas 244     /** Reads the next integer from the buffer.
11 13 Sep 07 nicklas 245      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 246      * @param offset The offset to the file.
11 13 Sep 07 nicklas 247      * @return The next integer value.
11 13 Sep 07 nicklas 248      */
11 13 Sep 07 nicklas 249     public static int MmGetInt32_I(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 250         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 251         return b.getInt();
11 13 Sep 07 nicklas 252     }
11 13 Sep 07 nicklas 253     
11 13 Sep 07 nicklas 254     /** Reads the next short from the buffer.
11 13 Sep 07 nicklas 255      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 256      * @param offset The offset to the file.
11 13 Sep 07 nicklas 257      * @return The next short value.
11 13 Sep 07 nicklas 258      */
11 13 Sep 07 nicklas 259     public static short MmGetUInt16_I(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 260         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 261         return b.getShort();
11 13 Sep 07 nicklas 262     }
11 13 Sep 07 nicklas 263     
11 13 Sep 07 nicklas 264     /** Reads the next short from the buffer.
11 13 Sep 07 nicklas 265      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 266      * @param offset The offset to the file.
11 13 Sep 07 nicklas 267      * @return The next short value.
11 13 Sep 07 nicklas 268      */
11 13 Sep 07 nicklas 269     public static short MmGetInt16_I(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 270         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 271         return b.getShort();
11 13 Sep 07 nicklas 272     }
11 13 Sep 07 nicklas 273     
11 13 Sep 07 nicklas 274     /** Reads the next byte from the buffer.
11 13 Sep 07 nicklas 275      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 276      * @param offset The offset to the file.
11 13 Sep 07 nicklas 277      * @return The next byte value.
11 13 Sep 07 nicklas 278      */
11 13 Sep 07 nicklas 279     public static byte MmGetUInt8(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 280         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 281         return b.get();
11 13 Sep 07 nicklas 282     }
11 13 Sep 07 nicklas 283     
11 13 Sep 07 nicklas 284     /** Reads the next byte from the buffer.
11 13 Sep 07 nicklas 285      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 286      * @param offset The offset to the file.
11 13 Sep 07 nicklas 287      * @return The next byte value.
11 13 Sep 07 nicklas 288      */
11 13 Sep 07 nicklas 289     public static byte MmGetInt8(MappedByteBuffer buffer, int offset) {
11 13 Sep 07 nicklas 290         ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 291         return b.get();
11 13 Sep 07 nicklas 292     }
11 13 Sep 07 nicklas 293     
11 13 Sep 07 nicklas 294     /** Read a string of fixed length.
11 13 Sep 07 nicklas 295      * @param buffer The buffered reader.
11 13 Sep 07 nicklas 296      * @param offset The offset to the file.
11 13 Sep 07 nicklas 297      * @param len The string length.
11 13 Sep 07 nicklas 298      * @return The string.
11 13 Sep 07 nicklas 299      */
11 13 Sep 07 nicklas 300     public static String MmGetFixedString_I(MappedByteBuffer buffer, int offset, int len) {
11 13 Sep 07 nicklas 301         try
11 13 Sep 07 nicklas 302         {
11 13 Sep 07 nicklas 303             String str = "";
11 13 Sep 07 nicklas 304             byte bvalue;
11 13 Sep 07 nicklas 305             ByteBuffer b = (ByteBuffer)buffer.position(offset);
11 13 Sep 07 nicklas 306             for (int i=0; i<len; i++)
11 13 Sep 07 nicklas 307             {
11 13 Sep 07 nicklas 308                 bvalue = b.get();
11 13 Sep 07 nicklas 309                 if (bvalue == 0)
11 13 Sep 07 nicklas 310                     break;
11 13 Sep 07 nicklas 311                 str += (char) bvalue;
11 13 Sep 07 nicklas 312             }
11 13 Sep 07 nicklas 313             return str;
11 13 Sep 07 nicklas 314         }
11 13 Sep 07 nicklas 315         catch (Throwable t)
11 13 Sep 07 nicklas 316         {
11 13 Sep 07 nicklas 317             return "";
11 13 Sep 07 nicklas 318         }
11 13 Sep 07 nicklas 319     }
14 14 Sep 07 nicklas 320     
14 14 Sep 07 nicklas 321     /**
14 14 Sep 07 nicklas 322      * Copy data from one stream to another.
14 14 Sep 07 nicklas 323      * @param in The stream to read from
14 14 Sep 07 nicklas 324      * @param out The stream to write to
14 14 Sep 07 nicklas 325      * @return The number of bytes copied
14 14 Sep 07 nicklas 326      * @throws IOException If there is an IO error
14 14 Sep 07 nicklas 327      */
14 14 Sep 07 nicklas 328     public static long copy(InputStream in, OutputStream out)
14 14 Sep 07 nicklas 329         throws IOException
14 14 Sep 07 nicklas 330     {
14 14 Sep 07 nicklas 331         int bytes = 0;
14 14 Sep 07 nicklas 332         long totalBytes = 0;
14 14 Sep 07 nicklas 333         byte[] buffer = new byte[1024];
14 14 Sep 07 nicklas 334     
14 14 Sep 07 nicklas 335         while (bytes != -1) // -1 = end of stream
14 14 Sep 07 nicklas 336         {
14 14 Sep 07 nicklas 337             bytes = in.read(buffer, 0, buffer.length);
14 14 Sep 07 nicklas 338             if (bytes > 0)
14 14 Sep 07 nicklas 339             {
14 14 Sep 07 nicklas 340                 totalBytes += bytes;
14 14 Sep 07 nicklas 341                 out.write(buffer, 0, bytes);
14 14 Sep 07 nicklas 342             }
14 14 Sep 07 nicklas 343         }
14 14 Sep 07 nicklas 344         return totalBytes;
11 13 Sep 07 nicklas 345 }
14 14 Sep 07 nicklas 346 }