本文整理汇总了Java中java.io.InputStream.read方法的典型用法代码示例。如果您正苦于以下问题:Java InputStream.read方法的具体用法?Java InputStream.read怎么用?Java InputStream.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.InputStream
的用法示例。
在下文中一共展示了InputStream.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyFile
import java.io.InputStream; //导入方法依赖的package包/类
public static void copyFile(String oldPathFile, String newPathFile) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPathFile);
if (oldfile.exists()) { // 文件存在
InputStream inStream = new FileInputStream(oldPathFile); // 读入源文�?
File n = new File(newPathFile);
if (!n.exists()) {
n.createNewFile();
}
FileOutputStream fs = new FileOutputStream(newPathFile);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节 文件大小
fs.write(buffer, 0, byteread);
}
fs.flush();
fs.close();
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例2: input2OutputStream
import java.io.InputStream; //导入方法依赖的package包/类
/**
* inputStream转outputStream
*
* @param is 输入流
* @return outputStream子类
*/
public static ByteArrayOutputStream input2OutputStream(InputStream is) {
if (is == null) return null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[MemoryConstants.KB];
int len;
while ((len = is.read(b, 0, MemoryConstants.KB)) != -1) {
os.write(b, 0, len);
}
return os;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(is);
}
}
示例3: getUrlBytes
import java.io.InputStream; //导入方法依赖的package包/类
public byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException(connection.getResponseMessage() +
": with " +
urlSpec);
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
示例4: injectDraculaCSS
import java.io.InputStream; //导入方法依赖的package包/类
private void injectDraculaCSS(String mode) {
try {
InputStream inputStream = getAssets().open("dracula.css");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
webView.loadUrl("javascript:(function() {var parent = document.getElementsByTagName('head').item(0);var style = document.createElement('style');style.type = 'text/css';style.innerHTML = window.atob('" + Base64.encodeToString(buffer, 2) + "');" + "parent.appendChild(style)" + "})()");
} catch (Exception d) {
d.printStackTrace();
}
}
示例5: drain
import java.io.InputStream; //导入方法依赖的package包/类
public static void drain(InputStream inStr)
throws IOException
{
byte[] bs = new byte[BUFFER_SIZE];
while (inStr.read(bs, 0, bs.length) >= 0)
{
}
}
示例6: read
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Simple wrapper around {@link InputStream#read()} that throws EOFException
* instead of returning -1.
*/
private static int read(InputStream is) throws IOException {
int b = is.read();
if (b == -1) {
throw new EOFException();
}
return b;
}
示例7: readKey
import java.io.InputStream; //导入方法依赖的package包/类
public AsymmetricKeyParameter readKey(InputStream stream)
throws IOException
{
byte[] V = new byte[(dhParams.getP().bitLength() + 7) / 8];
stream.read(V, 0, V.length);
return new DHPublicKeyParameters(new BigInteger(1, V), dhParams);
}
示例8: readResponse
import java.io.InputStream; //导入方法依赖的package包/类
public static String readResponse(InputStream stream) throws IOException {
byte[] data = new byte[100];
int read;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((read = stream.read(data)) != -1) {
out.write(data, 0, read);
}
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
示例9: AcceptSecContextToken
import java.io.InputStream; //导入方法依赖的package包/类
/**
* Creates an AcceptSecContextToken at the context initiator's side
* using the bytes received from the acceptor.
*/
public AcceptSecContextToken(Krb5Context context,
Credentials serviceCreds, KrbApReq apReq,
InputStream is)
throws IOException, GSSException, KrbException {
int tokenId = ((is.read()<<8) | is.read());
if (tokenId != Krb5Token.AP_REP_ID)
throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
"AP_REP token id does not match!");
byte[] apRepBytes =
new sun.security.util.DerValue(is).toByteArray();
KrbApRep apRep = new KrbApRep(apRepBytes, serviceCreds, apReq);
/*
* Allow the context acceptor to set a subkey if desired, even
* though our context acceptor will not do so.
*/
EncryptionKey subKey = apRep.getSubKey();
if (subKey != null) {
context.setKey(Krb5Context.ACCEPTOR_SUBKEY, subKey);
/*
System.out.println("\n\nSub-Session key from AP-REP is: " +
getHexBytes(subKey.getBytes()) + "\n");
*/
}
Integer apRepSeqNumber = apRep.getSeqNumber();
int peerSeqNumber = (apRepSeqNumber != null ?
apRepSeqNumber.intValue() :
0);
context.resetPeerSequenceNumber(peerSeqNumber);
}
示例10: injectHide
import java.io.InputStream; //导入方法依赖的package包/类
private void injectHide(String mode) {
try {
InputStream inputStream = getAssets().open("hidepeople.css");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
webView.loadUrl("javascript:(function() {var parent = document.getElementsByTagName('head').item(0);var style = document.createElement('style');style.type = 'text/css';style.innerHTML = window.atob('" + Base64.encodeToString(buffer, 2) + "');" + "parent.appendChild(style)" + "})()");
} catch (Exception d) {
d.printStackTrace();
}
}
示例11: checkExceptions
import java.io.InputStream; //导入方法依赖的package包/类
public static boolean checkExceptions(InputStream in) throws IOException {
String tempString;
int count = in.available();
boolean exception = false;
while (count > 0) {
byte[] b = new byte[count];
in.read(b);
tempString = new String(b);
if (!exception) {
exception = tempString.contains("RunTimeException");
}
count = in.available();
}
return exception;
}
示例12: recvPackage
import java.io.InputStream; //导入方法依赖的package包/类
/**
* receive whole pack
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return PkgInfo: status and reponse body(byte buff)
*/
public static PkgInfo recvPackage(InputStream in, byte expect_cmd, long expect_body_len) throws IOException
{
PkgHeader header = recvHeader(in, expect_cmd, expect_body_len);
if (header.status != 0)
{
return new PkgInfo(header, null);
}
byte[] body = new byte[header.body_len];
int totalBytes = 0;
int remainBytes = header.body_len;
int bytes;
while (totalBytes < header.body_len)
{
if ((bytes=in.read(body, totalBytes, remainBytes)) < 0)
{
break;
}
totalBytes += bytes;
remainBytes -= bytes;
}
if (totalBytes != header.body_len)
{
throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
}
return new PkgInfo(header, body);
}
示例13: readOneRequest
import java.io.InputStream; //导入方法依赖的package包/类
static void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
break;
}
} else {
requestEndCount = 0;
}
}
}
示例14: copyResource
import java.io.InputStream; //导入方法依赖的package包/类
private static void copyResource(CordovaResourceApi.OpenForReadResult input, OutputStream outputStream) throws IOException {
try {
InputStream inputStream = input.inputStream;
if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
FileChannel inChannel = ((FileInputStream)input.inputStream).getChannel();
FileChannel outChannel = ((FileOutputStream)outputStream).getChannel();
long offset = 0;
long length = input.length;
if (input.assetFd != null) {
offset = input.assetFd.getStartOffset();
}
// transferFrom()'s 2nd arg is a relative position. Need to set the absolute
// position first.
inChannel.position(offset);
outChannel.transferFrom(inChannel, 0, length);
} else {
final int BUFFER_SIZE = 8192;
byte[] buffer = new byte[BUFFER_SIZE];
for (;;) {
int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);
if (bytesRead <= 0) {
break;
}
outputStream.write(buffer, 0, bytesRead);
}
}
} finally {
input.inputStream.close();
if (outputStream != null) {
outputStream.close();
}
}
}
示例15: readDouble
import java.io.InputStream; //导入方法依赖的package包/类
public static double readDouble(final InputStream stream)
throws IOException {
long res = stream.read();
res |= (long) stream.read() << 8;
res |= (long) stream.read() << 16;
res |= (long) stream.read() << 24;
res |= (long) stream.read() << 32;
res |= (long) stream.read() << 40;
res |= (long) stream.read() << 48;
res |= (long) stream.read() << 56;
return Double.longBitsToDouble(res);
}