2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
10 * @version $Id: WWIO.java 1869 2007-05-23 22:18:47Z tgaskins $
12 package gov
.nasa
.worldwind
;
16 import java
.nio
.channels
.*;
18 import java
.util
.zip
.*;
22 public static boolean saveBuffer(ByteBuffer buffer
, File file
) throws IOException
26 String message
= WorldWind
.retrieveErrMsg("nullValue.BufferNull");
27 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
28 throw new IllegalArgumentException(message
);
33 String message
= WorldWind
.retrieveErrMsg("nullValue.FileIsNull");
34 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
35 throw new IllegalArgumentException(message
);
38 FileOutputStream fos
= null;
39 FileChannel channel
= null;
41 int numBytesWritten
= 0;
44 fos
= new FileOutputStream(file
);
45 channel
= fos
.getChannel();
46 lock
= channel
.tryLock();
49 // The file is being written to, or some other process is keeping it to itself.
50 // This is an okay condition, but worth noting.
51 String message
= WorldWind
.retrieveErrMsg("WWIO.UnableToAcquireLockFor") + file
.getPath();
52 WorldWind
.logger().log(java
.util
.logging
.Level
.FINER
, message
);
56 for (buffer
.rewind(); buffer
.hasRemaining();)
58 numBytesWritten
+= channel
.write(buffer
);
65 String message
= WorldWind
.retrieveErrMsg("WWIO.ErrorSavingBufferTo") + file
.getPath();
66 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
, e
);
68 if (numBytesWritten
> 0) // don't leave behind incomplete files
78 channel
.close(); // also releases the lock
82 catch (java
.io
.IOException e
)
84 String message
= WorldWind
.retrieveErrMsg("WWIO.ErrorTryingToClose") + file
.getPath();
85 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
90 public static MappedByteBuffer
mapFile(File file
) throws IOException
94 String message
= WorldWind
.retrieveErrMsg("nullValue.FileIsNull");
95 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
96 throw new IllegalArgumentException(message
);
99 FileInputStream is
= new FileInputStream(file
);
102 return is
.getChannel().map(FileChannel
.MapMode
.READ_ONLY
, 0, file
.length());
110 public static ByteBuffer
readURLContentToBuffer(URL url
) throws IOException
114 String message
= WorldWind
.retrieveErrMsg("nullValue.URLIsNull");
115 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
116 throw new IllegalArgumentException(message
);
119 InputStream is
= url
.openStream();
120 return readStreamToBuffer(is
);
121 // ByteBuffer buffer;
124 // URLConnection connection = url.openConnection();
125 // int size = connection.getContentLength();
126 // buffer = ByteBuffer.allocateDirect(size);
127 // byte inputBuffer[] = new byte[size];
131 // n = is.read(inputBuffer);
133 // buffer.put(inputBuffer, 0, n);
134 // } while (buffer.hasRemaining() && n >= 0);
149 public static ByteBuffer
readFileToBuffer(File file
) throws IOException
153 String message
= WorldWind
.retrieveErrMsg("nullValue.FileIsNull");
154 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
155 throw new IllegalArgumentException(message
);
158 FileInputStream is
= new FileInputStream(file
);
161 FileChannel fc
= is
.getChannel();
162 ByteBuffer buffer
= ByteBuffer
.allocate((int) fc
.size());
163 for (int count
= 0; count
>= 0 && buffer
.hasRemaining();)
165 count
= fc
.read(buffer
);
176 public static ByteBuffer
readZipEntryToBuffer(File zipFile
, String entryName
) throws IOException
180 String message
= WorldWind
.retrieveErrMsg("nullValue.FileIsNull");
181 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
182 throw new IllegalArgumentException(message
);
185 InputStream is
= null;
189 ZipFile zf
= new ZipFile(zipFile
);
192 String message
= WorldWind
.retrieveErrMsg("WWIO.ZipFileIsEmpty") + zipFile
.getPath();
193 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
194 throw new java
.io
.IOException(message
);
197 if (entryName
!= null)
198 { // Read the specified entry
199 ze
= zf
.getEntry(entryName
);
202 String message
= WorldWind
.retrieveErrMsg("WWIO.ZipFileEntryNIF") + entryName
203 + WorldWind
.retrieveErrMsg("punctuation.space") + zipFile
.getPath();
204 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
205 throw new IOException(message
);
209 { // Read the first entry
210 ze
= zf
.entries().nextElement(); // get the first entry
213 is
= zf
.getInputStream(ze
);
214 ByteBuffer buffer
= null;
215 if (ze
.getSize() > 0)
217 buffer
= transferStreamToByteBuffer(is
, (int) ze
.getSize());
229 private static ByteBuffer
transferStreamToByteBuffer(InputStream stream
, int numBytes
) throws IOException
233 String message
= WorldWind
.retrieveErrMsg("nullValue.InputStreamIsNull");
234 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
235 throw new IllegalArgumentException(message
);
240 String message
= WorldWind
.retrieveErrMsg("WWIO.NumberBytesTransferLessThanOne");
241 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
242 throw new IllegalArgumentException(message
);
247 byte[] bytes
= new byte[numBytes
];
248 while (count
>= 0 && (numBytes
- bytesRead
) > 0)
250 count
= stream
.read(bytes
, bytesRead
, numBytes
- bytesRead
);
256 return ByteBuffer
.wrap(bytes
);
259 public static ByteBuffer
readStreamToBuffer(InputStream inputStream
) throws IOException
261 final int PAGE_SIZE
= 8192;
263 ReadableByteChannel channel
= Channels
.newChannel(inputStream
);
264 ByteBuffer buffer
= ByteBuffer
.allocate(PAGE_SIZE
);
269 count
= channel
.read(buffer
);
270 if (count
> 0 && !buffer
.hasRemaining())
272 ByteBuffer biggerBuffer
= ByteBuffer
.allocate(buffer
.limit() + PAGE_SIZE
);
273 biggerBuffer
.put((ByteBuffer
) buffer
.rewind());
274 buffer
= biggerBuffer
;
284 public static String
replaceSuffix(String in
, String newSuffix
)
288 String message
= WorldWind
.retrieveErrMsg("nullValue.InputFileNameIsNull");
289 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
290 throw new IllegalArgumentException(message
);
293 return in
.substring(0, in
.lastIndexOf(".")) + (newSuffix
!= null ? newSuffix
: "");
296 public static File
saveBufferToTempFile(ByteBuffer buffer
, String suffix
) throws IOException
300 String message
= WorldWind
.retrieveErrMsg("nullValue.ByteBufferIsNull");
301 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
302 throw new IllegalArgumentException(message
);
305 File outputFile
= java
.io
.File
.createTempFile("WorldWind", suffix
!= null ? suffix
: "");
306 outputFile
.deleteOnExit();
308 WWIO
.saveBuffer(buffer
, outputFile
);
313 public static String
getSuffixForMimeType(String mimeType
)
315 if (mimeType
== null)
317 String msg
= WorldWind
.retrieveErrMsg("nullValue.MimeTypeIsNull");
318 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, msg
);
319 throw new IllegalArgumentException(msg
);
322 if (mimeType
.equalsIgnoreCase("image/jpeg") || mimeType
.equalsIgnoreCase("image/jpg"))
324 if (mimeType
.equalsIgnoreCase("image/png"))
330 public static boolean isFileOutOfDate(URL url
, long expiryTime
)
334 String message
= WorldWind
.retrieveErrMsg("nullValue.URLIsNull");
335 gov
.nasa
.worldwind
.WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
336 throw new IllegalArgumentException(message
);
341 // Determine whether the file can be treated like a File, e.g., a jar entry.
342 URI uri
= url
.toURI();
344 return false; // TODO: Determine how to check the date of non-Files
346 File file
= new File(uri
);
348 return file
.exists() && file
.lastModified() < expiryTime
;
350 catch (URISyntaxException e
)
352 String message
= WorldWind
.retrieveErrMsg("WWIO.ExceptionValidatingFileExpiration");
353 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
+ url
);