本文整理汇总了Java中java.io.InputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java InputStream.close方法的具体用法?Java InputStream.close怎么用?Java InputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.InputStream
的用法示例。
在下文中一共展示了InputStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doLocalXMLParser
import java.io.InputStream; //导入方法依赖的package包/类
private void doLocalXMLParser(){
try {
//Return an AssetManager instance for your application's package
InputStream is = getAssets().open("test.xml");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String xmlData = new String(buffer, "UTF-8");
beginParser(xmlData);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
示例2: getCMDOutputString
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Get command output string.
*/
public static String getCMDOutputString(String[] args) {
try {
ProcessBuilder cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
StringBuilder sb = new StringBuilder();
byte[] re = new byte[64];
int len;
while ((len = in.read(re)) != -1) {
sb.append(new String(re, 0, len));
}
in.close();
process.destroy();
JLog.i("CMD: " + sb.toString());
return sb.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
示例3: loadData
import java.io.InputStream; //导入方法依赖的package包/类
protected void loadData ( final File file ) throws Exception
{
if ( OscarLoader.isOscar ( file ) )
{
final OscarLoader loader = new OscarLoader ( file );
this.mergeController.setLocalData ( loader.getData () );
this.mergeController.setIgnoreFields ( loader.getIgnoreFields () );
}
else
{
final InputStream stream = new FileInputStream ( file );
try
{
this.mergeController.setLocalData ( OscarLoader.loadJsonData ( stream ) );
}
finally
{
stream.close ();
}
}
}
示例4: refresh
import java.io.InputStream; //导入方法依赖的package包/类
public static boolean refresh(){
props = new Properties();
try {
URLConnection connection = new URL("http://kussmaul.net/projects.properties").openConnection();
connection.setConnectTimeout(1000);
final InputStream in = connection.getInputStream();
props.load(in);
if(!props.containsKey("ServerIP"))
throw new Exception("Something wrong with download");
in.close();
return true;
} catch (Exception e) {
e.printStackTrace();
props = getDefaultProperties();
return false;
}
}
示例5: exportResource
import java.io.InputStream; //导入方法依赖的package包/类
public static String exportResource(Context context, int resourceId, String dirname) {
String fullname = context.getResources().getString(resourceId);
String resName = fullname.substring(fullname.lastIndexOf("/") + 1);
try {
InputStream is = context.getResources().openRawResource(resourceId);
File resDir = context.getDir(dirname, Context.MODE_PRIVATE);
File resFile = new File(resDir, resName);
FileOutputStream os = new FileOutputStream(resFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
return resFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
throw new CvException("Failed to export resource " + resName
+ ". Exception thrown: " + e);
}
}
示例6: testHadoopFileSystem
import java.io.InputStream; //导入方法依赖的package包/类
@Test
@TestHdfs
public void testHadoopFileSystem() throws Exception {
Configuration conf = TestHdfsHelper.getHdfsConf();
FileSystem fs = FileSystem.get(conf);
try {
OutputStream os = fs.create(new Path(TestHdfsHelper.getHdfsTestDir(), "foo"));
os.write(new byte[]{1});
os.close();
InputStream is = fs.open(new Path(TestHdfsHelper.getHdfsTestDir(), "foo"));
assertEquals(is.read(), 1);
assertEquals(is.read(), -1);
is.close();
} finally {
fs.close();
}
}
示例7: inputstreamToByteArray
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Converts an InputStream into a byte array.
*
* @param ins input stream to convert
*
* @return resultant byte array
*
* @throws MetadataProviderException thrown if there is a problem reading the resultant byte array
*/
protected byte[] inputstreamToByteArray(InputStream ins) throws MetadataProviderException {
try {
// 1 MB read buffer
byte[] buffer = new byte[1024 * 1024];
ByteArrayOutputStream output = new ByteArrayOutputStream();
int n = 0;
while (-1 != (n = ins.read(buffer))) {
output.write(buffer, 0, n);
}
ins.close();
return output.toByteArray();
} catch (IOException e) {
throw new MetadataProviderException(e);
}
}
示例8: close
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Closes stream associated with current thread.
*
* @throws IOException if an error occurs
*/
@Override
public void close()
throws IOException
{
InputStream input = m_streams.get();
if( null != input )
{
input.close();
}
}
示例9: copyFile
import java.io.InputStream; //导入方法依赖的package包/类
private static void copyFile(File file, InputStream is, String mode)
throws IOException, InterruptedException {
final String abspath = file.getAbsolutePath();
final FileOutputStream out = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
is.close();
Runtime.getRuntime().exec("chmod " + mode + " " + abspath).waitFor();
}
示例10: testFileInputStream
import java.io.InputStream; //导入方法依赖的package包/类
@Test
public void testFileInputStream() throws IOException {
InputStream is = new FileInputStream(file);
assertFalse(is.markSupported());
final String content = IOUtils.toString(is);
final String content2 = IOUtils.toString(is);
assertTrue(content.length() == 100);
assertEquals(content2, "");
is.close();
}
示例11: copy
import java.io.InputStream; //导入方法依赖的package包/类
private int copy(InputStream in, RandomAccessFile out) throws IOException, UpdateError {
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream bis = new BufferedInputStream(in, BUFFER_SIZE);
try {
out.seek(out.length());
int bytes = 0;
long previousBlockTime = -1;
while (!isCancelled()) {
int n = bis.read(buffer, 0, BUFFER_SIZE);
if (n == -1) {
break;
}
out.write(buffer, 0, n);
bytes += n;
checkNetwork();
if (mSpeed != 0) {
previousBlockTime = -1;
} else if (previousBlockTime == -1) {
previousBlockTime = System.currentTimeMillis();
} else if ((System.currentTimeMillis() - previousBlockTime) > TIME_OUT) {
throw new UpdateError(UpdateError.DOWNLOAD_NETWORK_TIMEOUT);
}
}
return bytes;
} finally {
out.close();
bis.close();
in.close();
}
}
示例12: copy
import java.io.InputStream; //导入方法依赖的package包/类
public static void copy(InputStream in, File file) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: addOTACert
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Add a copy of the public key to the archive; this should exactly match one
* of the files in /system/etc/security/otacerts.zip on the device. (The same
* cert can be extracted from the CERT.RSA file but this is much easier to get
* at.)
*/
private static void addOTACert(
JarOutputStream outputJar,
long timestamp,
Manifest manifest)
throws IOException, GeneralSecurityException {
InputStream input = new ByteArrayInputStream(readKeyContents(PUBLIC_KEY));
BASE64Encoder base64 = new BASE64Encoder();
MessageDigest md = MessageDigest.getInstance("SHA1");
JarEntry je = new JarEntry(OTACERT_NAME);
je.setTime(timestamp);
outputJar.putNextEntry(je);
byte[] b = new byte[4096];
int read;
System.out.print("\rGenerating OTA certificate...");
while ((read = input.read(b)) != -1) {
outputJar.write(b, 0, read);
md.update(b, 0, read);
}
input.close();
Attributes attr = new Attributes();
attr.putValue("SHA1-Digest", base64.encode(md.digest()));
manifest.getEntries().put(OTACERT_NAME, attr);
}
示例14: getImageFromAssetsFile
import java.io.InputStream; //导入方法依赖的package包/类
private static Bitmap getImageFromAssetsFile(Context context,String fileName){
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}catch (IOException e){
e.printStackTrace();
}
return image;
}
示例15: writeTo
import java.io.InputStream; //导入方法依赖的package包/类
public void writeTo(final OutputStream outstream) throws IOException {
Args.notNull(outstream, "Output stream");
final InputStream instream = new FileInputStream(this.file);
try {
final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
int l;
while ((l = instream.read(tmp)) != -1) {
outstream.write(tmp, 0, l);
}
outstream.flush();
} finally {
instream.close();
}
}