Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / formats / nitfs / NitfsUtil.java
bloba89c0876c30e039877f3bd71efd3ed389df5c5eb
1 package gov.nasa.worldwind.formats.nitfs;
3 import gov.nasa.worldwind.*;
5 import java.io.*;
6 import java.nio.channels.*;
7 import java.nio.*;
8 /*
9 Copyright (C) 2001, 2007 United States Government
10 as represented by the Administrator of the
11 National Aeronautics and Space Administration.
12 All Rights Reserved.
15 /**
16 * @author Lado Garakanidze
17 * @version $Id: NitfsUtil Mar 30, 2007 12:43:29 PM lado
19 public class NitfsUtil
21 public static String getString(java.nio.ByteBuffer buffer, int offset, int len)
23 String s = StringUtil.EMPTY;
24 if (null != buffer && buffer.capacity() >= offset + len)
26 byte[] dest = new byte[len];
27 buffer.position(offset);
28 buffer.get(dest, 0, len);
29 s = new String(dest).trim();
31 return s;
34 public static String getString(java.nio.ByteBuffer buffer, int len)
36 String s = StringUtil.EMPTY;
37 if (null != buffer && buffer.remaining() >= len)
39 byte[] dest = new byte[len];
40 buffer.get(dest, 0, len);
41 s = new String(dest).trim();
43 return s;
46 public static int getNumeric(java.nio.ByteBuffer buffer, int len)
48 String s = StringUtil.EMPTY;
49 if (null != buffer && buffer.remaining() >= len)
51 byte[] dest = new byte[len];
52 buffer.get(dest, 0, len);
53 s = new String(dest);
55 return Integer.parseInt(s);
58 public static short getShortNumeric(java.nio.ByteBuffer buffer, int len)
60 String s = StringUtil.EMPTY;
61 if (null != buffer && buffer.remaining() >= len)
63 byte[] dest = new byte[len];
64 buffer.get(dest, 0, len);
65 s = new String(dest);
67 return (short) (0xFFFF & Integer.parseInt(s));
70 public static boolean getBoolean(java.nio.ByteBuffer buffer)
72 return !((byte) 0 == buffer.get()); // 0 = false, non-zero = true
75 public static short getByteAsShort(java.nio.ByteBuffer buffer)
77 return (short) (0xFF & buffer.get());
80 public static int getUShort(java.nio.ByteBuffer buffer)
82 return 0xFFFF & buffer.getShort();
85 public static long getUInt(java.nio.ByteBuffer buffer)
87 return 0xFFFFFFFFL & (long) buffer.getInt();
90 private static final int PAGE_SIZE = 4096;
93 public static java.nio.ByteBuffer readEntireFile(java.io.File file) throws java.io.IOException
95 return NitfsUtil.memoryMapFile(file);
96 // return NitfsUtil.readFile(file);
99 private static java.nio.ByteBuffer readFile(java.io.File file) throws java.io.IOException
101 java.io.FileInputStream fis = new java.io.FileInputStream(file);
102 java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(PAGE_SIZE);
103 java.nio.channels.ReadableByteChannel channel = java.nio.channels.Channels.newChannel(fis);
105 int count = 0;
106 while (count >= 0)
108 count = channel.read(buffer);
109 if (count > 0 && !buffer.hasRemaining())
111 java.nio.ByteBuffer biggerBuffer = java.nio.ByteBuffer.allocate(buffer.limit() + PAGE_SIZE);
112 biggerBuffer.put((java.nio.ByteBuffer) buffer.rewind());
113 buffer = biggerBuffer;
117 if (buffer != null)
118 buffer.flip();
120 return buffer;
123 private static java.nio.ByteBuffer memoryMapFile(java.io.File file) throws IOException
125 FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
126 long fileSize = roChannel.size();
127 MappedByteBuffer mapFile = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
128 if (!mapFile.isLoaded())
129 mapFile.load();
130 roChannel.close();
131 return mapFile;