/** * Copyright 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package com.jogamp.common.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.AccessController; import java.net.JarURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; import jogamp.common.Debug; import jogamp.common.os.android.StaticContext; import android.content.Context; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.AndroidVersion; import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.Platform; public class IOUtil { private static final boolean DEBUG = Debug.isPropertyDefined("jogamp.debug.IOUtil", true, AccessController.getContext()); private IOUtil() {} /*** * * STREAM COPY STUFF * */ /** * Copy the specified input stream to the specified output file. The total * number of bytes written is returned. Both streams are closed upon completion. */ public static int copyURL2File(URL url, File outFile) throws IOException { URLConnection conn = url.openConnection(); conn.connect(); int totalNumBytes = 0; InputStream in = new BufferedInputStream(conn.getInputStream()); try { OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); try { totalNumBytes = copyStream2Stream(in, out, conn.getContentLength()); } finally { out.close(); } } finally { in.close(); } return totalNumBytes; } /** * Copy the specified input stream to the specified output stream. The total * number of bytes written is returned. * * @param in the source * @param out the destination * @param totalNumBytes informal number of expected bytes, maybe used for user feedback while processing. -1 if unknown * @return * @throws IOException */ public static int copyStream2Stream(InputStream in, OutputStream out, int totalNumBytes) throws IOException { final byte[] buf = new byte[Platform.getMachineDescription().pageSizeInBytes()]; int numBytes = 0; while (true) { int count; if ((count = in.read(buf)) == -1) { break; } out.write(buf, 0, count); numBytes += count; } return numBytes; } /** * Copy the specified input stream to a byte array, which is being returned. */ public static byte[] copyStream2ByteArray(InputStream stream) throws IOException { // FIXME: Shall enforce a BufferedInputStream ? if( !(stream instanceof BufferedInputStream) ) { stream = new BufferedInputStream(stream); } int totalRead = 0; int avail = stream.available(); byte[] data = new byte[avail]; int numRead = 0; do { if (totalRead + avail > data.length) { final byte[] newData = new byte[totalRead + avail]; System.arraycopy(data, 0, newData, 0, totalRead); data = newData; } numRead = stream.read(data, totalRead, avail); if (numRead >= 0) { totalRead += numRead; } avail = stream.available(); } while (avail > 0 && numRead >= 0); // just in case the announced avail > totalRead if (totalRead != data.length) { final byte[] newData = new byte[totalRead]; System.arraycopy(data, 0, newData, 0, totalRead); data = newData; } return data; } /** * Copy the specified input stream to a NIO ByteBuffer w/ native byte order, which is being returned. *
The implementation creates the ByteBuffer w/ {@link #copyStream2ByteArray(InputStream)}'s returned byte array.
*/ public static ByteBuffer copyStream2ByteBuffer(InputStream stream) throws IOException { // FIXME: Shall enforce a BufferedInputStream ? if( !(stream instanceof BufferedInputStream) ) { stream = new BufferedInputStream(stream); } int avail = stream.available(); final MachineDescription machine = Platform.getMachineDescription(); ByteBuffer data = Buffers.newDirectByteBuffer( machine.pageAlignedSize(avail) ); byte[] chunk = new byte[machine.pageSizeInBytes()]; int chunk2Read = Math.min(machine.pageSizeInBytes(), avail); int numRead = 0; do { if (avail > data.remaining()) { final ByteBuffer newData = Buffers.newDirectByteBuffer( machine.pageAlignedSize(data.position() + avail) ); newData.put(data); data = newData; } numRead = stream.read(chunk, 0, chunk2Read); if (numRead >= 0) { data.put(chunk, 0, numRead); } avail = stream.available(); chunk2Read = Math.min(machine.pageSizeInBytes(), avail); } while (avail > 0 && numRead >= 0); data.flip(); return data; } /*** * * RESOURCE / FILE NAME STUFF * */ /** * Returns the lowercase suffix of the given file name (the text * after the last '.' in the file name). Returns null if the file * name has no suffix. Only operates on the given file name; * performs no I/O operations. * * @param file name of the file * @return lowercase suffix of the file name * @throws NullPointerException if file is null */ public static String getFileSuffix(File file) { return getFileSuffix(file.getName()); } /** * Returns the lowercase suffix of the given file name (the text * after the last '.' in the file name). Returns null if the file * name has no suffix. Only operates on the given file name; * performs no I/O operations. * * @param filename name of the file * @return lowercase suffix of the file name * @throws NullPointerException if filename is null */ public static String getFileSuffix(String filename) { int lastDot = filename.lastIndexOf('.'); if (lastDot < 0) { return null; } return toLowerCase(filename.substring(lastDot + 1)); } public static String getClassFileName(String clazzBinName) throws IOException { // or return clazzBinName.replace('.', File.pathSeparatorChar) + ".class"; ? return clazzBinName.replace('.', '/') + ".class"; } /** * @param clazzBinName com.jogamp.common.util.cache.TempJarCache * @param cl * @return jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class * @throws IOException */ public static URL getClassURL(String clazzBinName, ClassLoader cl) throws IOException { return cl.getResource(getClassFileName(clazzBinName)); } /** * Returns the basename of the given fname w/o directory part */ public static String getBasename(String fname) { fname = fname.replace('\\', '/'); // unify file seperator int lios = fname.lastIndexOf('/'); // strip off dirname if(lios>=0) { fname = fname.substring(lios+1); } return fname; } /** * Returns unified '/' dirname including the last '/' */ public static String getDirname(String fname) { fname = fname.replace('\\', '/'); // unify file seperator int lios = fname.lastIndexOf('/'); // strip off dirname if(lios>=0) { fname = fname.substring(0, lios+1); } return fname; } private static String toLowerCase(String arg) { if (arg == null) { return null; } return arg.toLowerCase(); } /*** * * RESOURCE LOCATION STUFF * */ /** * Locating a resource using 'getResource(String path, ClassLoader cl)', * with the *java.io.tempdir
* is returned.
*
* On Android a temp
folder relative to the applications local folder
* (see {@link Context#getDir(String, int)}) is returned, if
* the Android application/activity has registered it's Application Context
* via {@link StaticContext#setContext(Context)}.
* This allows using the temp folder w/o the need for sdcard
* access, which would be the java.io.tempdir
location on Android!
*
* The purpose of this wrapper
is to allow unique code to be used
* for both platforms w/o the need to handle extra permissions.
*