Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (56 sloc)
1.7 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ### | |
* IP: Public Domain | |
*/ | |
package mobiledevices.dmg.ghidra; | |
import java.io.*; | |
public final class GFileUtilityMethods { | |
private static final String GHIDRA_FILE_SYSTEM_PREFIX = "ghidra_file_system_"; | |
private static final String GHIDRA_FILE_SYSTEM_SUFFIX = ".tmp"; | |
public final static File writeTemporaryFile( InputStream inputStream ) throws IOException { | |
return writeTemporaryFile( inputStream , Integer.MAX_VALUE ); | |
} | |
public final static File writeTemporaryFile( InputStream inputStream, int maxBytesToWrite ) throws IOException { | |
File tempOutputFile = File.createTempFile( GHIDRA_FILE_SYSTEM_PREFIX, GHIDRA_FILE_SYSTEM_SUFFIX ); | |
tempOutputFile.deleteOnExit(); | |
OutputStream outputStream = new FileOutputStream( tempOutputFile ); | |
try { | |
int nWritten = 0; | |
byte [] buffer = new byte[ 8192 ]; | |
while ( true ) { | |
int nRead = inputStream.read( buffer ); | |
if ( nRead == -1 ) { | |
break; | |
} | |
outputStream.write( buffer, 0, nRead ); | |
nWritten += nRead; | |
if ( nWritten >= maxBytesToWrite ) { | |
break; | |
} | |
} | |
} | |
finally { | |
outputStream.close(); | |
} | |
return tempOutputFile; | |
} | |
public final static File writeTemporaryFile( byte [] bytes, String prefix ) throws IOException { | |
if ( prefix == null ) { | |
prefix = GHIDRA_FILE_SYSTEM_PREFIX; | |
} | |
if ( prefix.length() < 3 ) {//temp file prefix must be at least 3 chars in length | |
for ( int i = prefix.length() ; i < 3 ; ++i ) { | |
prefix = prefix + '_'; | |
} | |
} | |
File tempFile = File.createTempFile( prefix , GHIDRA_FILE_SYSTEM_SUFFIX ); | |
tempFile.deleteOnExit(); | |
OutputStream tempFileOut = new FileOutputStream( tempFile ); | |
try { | |
tempFileOut.write( bytes ); | |
} | |
finally { | |
tempFileOut.close(); | |
} | |
return tempFile; | |
} | |
} |