本文整理匯總了Java中java.io.InputStream類的典型用法代碼示例。如果您正苦於以下問題:Java InputStream類的具體用法?Java InputStream怎麽用?Java InputStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InputStream類屬於java.io包,在下文中一共展示了InputStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: download
import java.io.InputStream; //導入依賴的package包/類
public File download(boolean delete, String link) throws IOException{
URL url = new URL(link);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
File f = new File(defaultFolder, link);
FileOutputStream out = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) > 0){
out.write(buffer, 0, len);
}
in.close();
out.close();
if(delete) f.deleteOnExit();
return f;
}
示例2: streamBaseXInfos
import java.io.InputStream; //導入依賴的package包/類
private void streamBaseXInfos(String graph, String version, InputStream inputStream, boolean isSegmentXInfo)
throws XInfoNotSupportedException, GraphImportException, GraphStorageException, GraphNotExistsException {
//First checkk if already another import is running. The singleton serverStatus has to be injected therefore
if (!serverStatus.registerImport()) {
throw new GraphImportException("Sorry, system is busy, a graph import is currently executed");
}
IBaseSegmentProducer<IBaseSegment> producer = null;
try {
BlockingQueue<IBaseSegment> segmentsQueue;
segmentsQueue = new ArrayBlockingQueue<>(queueSize);
producer = new BaseSegmentProducerImpl<>(inputFormat, inputStream, segmentsQueue);
Thread producerThread = new Thread(producer, "basesegment-xinfo-parser-thread");
producerThread.start();
List<IBaseSegment> segments = new ArrayList<>();
while (producerThread.isAlive() || !segmentsQueue.isEmpty()) {
if (!segmentsQueue.isEmpty()) {
segments.add(segmentsQueue.poll());
}
if (segments.size() >= this.batchSize) {
this.writeSegments(segments,graph,version,isSegmentXInfo);
segments.clear();
}
}
this.writeSegments(segments,graph,version,isSegmentXInfo);
} finally {
serverStatus.unregisterImport();
if (producer != null && producer.getException() != null) {
throw new GraphImportException("Graph could not be imported",producer.getException());
}
}
}
示例3: getMD5
import java.io.InputStream; //導入依賴的package包/類
private static String getMD5(final InputStream is, final int bufLen) {
if (is == null || bufLen <= 0) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance(Algorithm.MD5.getType());
StringBuilder md5Str = new StringBuilder(32);
byte[] buf = new byte[bufLen];
int readCount = 0;
while ((readCount = is.read(buf)) != -1) {
md.update(buf, 0, readCount);
}
byte[] hashValue = md.digest();
for (int i = 0; i < hashValue.length; i++) {
md5Str.append(Integer.toString((hashValue[i] & 0xff) + 0x100, 16).substring(1));
}
return md5Str.toString();
} catch (Exception e) {
return null;
}
}
示例4: 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);
}
}
示例5: testStorageModifyCommand
import java.io.InputStream; //導入依賴的package包/類
@Test
public void testStorageModifyCommand() throws IOException {
String text = "Tobato is a good man. this is a test of StorageTruncateCommand.";
InputStream firstIn = getTextInputStream(text);
long firstSize = firstIn.available();
// 上載文字
System.out.println(firstSize);
StorePath path = uploadInputStream(firstIn, "txt", firstSize, true);
// 文件修改
String Modifytext = "This is a test of StorageModifyCommand";
InputStream modifyIn = getTextInputStream(Modifytext);
long modifySize = modifyIn.available();
// 觀察運行效果:
// fileOffset參數0 結果為 This is a test of StorageModifyCommandf
// StorageTruncateCommand
// fileOffset參數為20 結果為 Tobato is a good manThis is a test of
// StorageModifyCommandmand
StorageModifyCommand command = new StorageModifyCommand(path.getPath(), modifyIn, modifySize, 0);
executeStoreCmd(command);
LOGGER.debug("--文件修改處理成功--");
}
示例6: testGetContentSegment
import java.io.InputStream; //導入依賴的package包/類
@Test
public void testGetContentSegment() {
final BinaryService resolver = new HttpBasedBinaryService(idService.getSupplier("http://example.org/"));
final Optional<InputStream> res = resolver.getContent(resource, singletonList(between(5, 20)));
assertTrue(res.isPresent());
final String str = res.map(this::uncheckedToString).get();
assertFalse(str.contains("owl:Ontology"));
assertEquals(16, str.length());
}
示例7: writeFile
import java.io.InputStream; //導入依賴的package包/類
/**
*
* @param file 書き込み先のファイルオブジェクト
* @param content 書き込む內容(InputStream)
*
* <p>writeFile - Utils</p>
*
* <p>contentで指定した內容をファイルに書き込みます。</p>
*
* Jupiter by Jupiter Development Team
*
*/
public static void writeFile(File file, InputStream content) throws IOException {
if (content == null) {
throw new IllegalArgumentException("content must not be null");
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = content.read(buffer)) != -1) {
stream.write(buffer, 0, length);
}
stream.close();
content.close();
}
示例8: pipe
import java.io.InputStream; //導入依賴的package包/類
private synchronized Thread pipe(InputStream is, OutputStream os, char tag) {
return new Thread("TunnelPipe("+tag+")") {
@Override
public void run() {
try {
try {
int c;
while ((c = is.read()) != -1) {
os.write(c);
os.flush();
// if DEBUG prints a + or a - for each transferred
// character.
if (DEBUG) System.out.print(tag);
}
is.close();
} finally {
os.close();
}
} catch (IOException ex) {
if (DEBUG) ex.printStackTrace(System.out);
}
}
};
}
示例9: getInputStream
import java.io.InputStream; //導入依賴的package包/類
private static InputStream getInputStream(URL jar) throws IOException {
//to fix offset caused by launch4j
InputStream is = jar.openStream();
Log.debug("Scanning for jar...");
long offset = 0;
boolean found = false;
try {
while (true) {
if (getUnsignedInt(is) == 0x04034b50L) {
found = true;
break;
}
offset += 4;
}
} catch (IOException ignored) {
}
is.close();
InputStream finalIS = jar.openStream();
if (!found) {
Log.debug("Failed to find start");
} else {
Log.debug("Skipping " + offset + " bytes until start of jar [" + finalIS.skip(offset) + "]");
if (finalIS.markSupported()) finalIS.mark(Integer.MAX_VALUE);
}
return finalIS;
}
示例10: post
import java.io.InputStream; //導入依賴的package包/類
/**
* 鍙戦�丳ost璿鋒眰
* @param url
* @param params
* @return
* @throws IOException
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂榪癝SLContext瀵矽薄涓緱鍒癝SLSocketFactory瀵矽薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 榪炴帴瓚呮椂
http.setConnectTimeout(50000);
// 璿誨彇瓚呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(50000);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
OutputStream out = http.getOutputStream();
out.write(params.getBytes("UTF-8"));
out.flush();
out.close();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴榪炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例11: getImage
import java.io.InputStream; //導入依賴的package包/類
/**
* Returns an {@link Image} encoded by the specified {@link InputStream}.
*
* @param stream
* the {@link InputStream} encoding the image data
* @return the {@link Image} encoded by the specified input stream
*/
protected static Image getImage(InputStream stream) throws IOException {
try {
Display display = Display.getCurrent();
ImageData data = new ImageData(stream);
if (data.transparentPixel > 0) {
return new Image(display, data, data.getTransparencyMask());
}
return new Image(display, data);
} finally {
stream.close();
}
}
示例12: check
import java.io.InputStream; //導入依賴的package包/類
static void check(Object source) throws Exception {
AudioFileFormat aff2 = null;
if (source instanceof File) {
aff2 = AudioSystem.getAudioFileFormat((File) source);
}
else if (source instanceof InputStream) {
aff2 = AudioSystem.getAudioFileFormat((InputStream) source);
}
else if (source instanceof URL) {
aff2 = AudioSystem.getAudioFileFormat((URL) source);
} else throw new Exception("wrong source. Test FAILED");
System.out.println("Got: "+aff2);
if (aff2.getFormat().getSampleSizeInBits()==-1) {
throw new Exception("wrong audio format. Test FAILED");
}
}
示例13: copyStream
import java.io.InputStream; //導入依賴的package包/類
private static void copyStream(InputStream is, OutputStream os)
throws IOException
{
byte[] copyBuf = new byte[8192];
int n;
while ((n = is.read(copyBuf)) != -1) {
os.write(copyBuf, 0, n);
}
}
示例14: getProperties
import java.io.InputStream; //導入依賴的package包/類
@Override
public Properties getProperties(String path) {
InputStream is = getInputStream(path);
Properties res = new Properties();
if (is != null) {
try {
res.loadFromXML(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return res;
}
示例15: readTemplateFromStream
import java.io.InputStream; //導入依賴的package包/類
/**
* reads a template from an inputstream
*/
private void readTemplateFromStream(String id, InputStream stream) throws IOException
{
NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(stream);
Template template = new Template();
template.read(nbttagcompound);
this.templates.put(id, template);
}