我在从Android平台上的InputStream读取时遇到了一个奇怪的问题.我不确定这是Android特定的问题,还是我一般都做错了.
唯一特定于Android的是此调用:
InputStream is = getResources().openRawResource(R.raw.myfile);
Run Code Online (Sandbox Code Playgroud)
这将从Android资产返回文件的InputStream.无论如何,这是我遇到问题的地方:
bytes[] buffer = new bytes[2];
is.read(buffer);
Run Code Online (Sandbox Code Playgroud)
当read()执行时抛出IOException.奇怪的是,如果我执行两个连续的单字节读取(或任意数量的单字节读取),则没有例外.例如,这有效:
byte buffer;
buffer = (byte)buffer.read();
buffer = (byte)buffer.read();
Run Code Online (Sandbox Code Playgroud)
任何想法为什么两个连续的单字节读取工作,但一次调用同时读取两个引发异常?该InputStream中似乎罚款... is.available()回报超过一百万个字节(因为它应该).
堆栈跟踪显示在以下行之前的这些行InputStream.read():
java.io.IOException
at android.content.res.AssetManager.readAsset(Native Method)
at android.content.res.AssetManager.access$800(AssetManager.java:36)
at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:542)
Run Code Online (Sandbox Code Playgroud)
将缓冲区大小更改为单个字节仍会引发错误.看起来只有在读入字节数组时才会引发异常.
如果我将文件截断为100,000字节(文件最初为:1,917,408字节),它可以正常工作.超过一定大小的文件有问题吗?
任何帮助表示赞赏!
谢谢!
我可以将任何InputStream写入FileChannel吗?
我正在使用java.nio.channels.FileChannel打开文件并将其锁定,然后将InputStream写入输出文件.InputStream可以由另一个文件,URL,套接字或任何东西打开.我写了以下代码:
FileOutputStream outputStream = new FileOutputStream(outputFile);
FileChannel outputChannel = outputStream.getChannel();
FileLock lock = outputChannel.lock();
try {
outputChannel.transferFrom(???);
} finally {
lock.release();
outputChannel.close();
outputStream.close();
}
Run Code Online (Sandbox Code Playgroud)
但是,outputChannel.transferFrom(...)的第一个参数请求ReadableByteChannel对象.由于我使用InputStream作为输入,因此它没有inputStream.getChannel()方法来创建所需的通道.
有没有办法从InputStream获取ReadableByteChannel?
我的代码:
BufferedInputStream bis =
new BufferedInputStream(getClass().getResourceAsStream("playerhit.mp3"));
Run Code Online (Sandbox Code Playgroud)
当playerhit.mp3文件与MP3.class运行时的包在同一个包中时,此代码可以正常工作.我将其作为.jar运行.如果我将文件路径更改为类似/src/data/audio/playerhit.mp3它不再起作用的东西.在以.jar身份运行时,是否仍然可以访问不同于文件包根目录的文件路径?
如何创建一个Thread安全的InputStream.在多线程操作中,inputStream数据被破坏,所以如何使我的inputStream线程安全.将使用以下代码工作
public class SynchronizedInputStream extends InputStream{
private InputStream in;
private SynchronizedInputStream( InputStream in ) {
this.in = in;
}
/* ... method for every InputStream type to use */
public static InputStream createInputStream( InputStream in) {
return new SynchronizedInputStream( in);
}
public static InputStream createPushBackInputStream(InputStream in,int BUFSIZE){
return new SynchronizedInputStream(new PushbackInputStream(in,BUFSIZE));
}
/* Wrap all InputStream methods Used */
public int read(){
synchronized (this) {
try {
return in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的客户端/服务器网络应用程序,通过TCP套接字发送和接收固定大小的消息.
到目前为止,我一直在使用getInputStream()与getOutputStream()该方法的Socket类来获取数据流,然后调用read(byte[] b, int off, int len)该方法InputStream的类读取每个时间(这是一个消息的大小),60个字节.
后来,我阅读了该方法的Javadoc:
public int read(byte [] b,int off,int len)抛出IOException
将输入流中最多 len个字节的数据读入一个字节数组.尝试读取len个字节,但可以读取较小的数字.实际读取的字节数以整数形式返回.
我想知道是否有任何Java"开箱即用"的解决方案要阻止,直到len字节被读取,必要时永远等待.
我显然可以创建一个简单的循环,但我觉得我正在重新发明轮子.你能建议我一个干净的Java感知解决方案吗?
我正在学习如何使用InputStream.我试图为BufferedInputStream使用mark,但是当我尝试重置时,我有以下异常:
java.io.IOException: Resetting to invalid mark
Run Code Online (Sandbox Code Playgroud)
我认为这意味着我的标记读取限制设置错误.我实际上不知道如何在mark()中设置读取限制.我试过这样的:
is = new BufferedInputStream(is);
is.mark(is.available());
Run Code Online (Sandbox Code Playgroud)
这也是错误的.
is.mark(16);
Run Code Online (Sandbox Code Playgroud)
这也引发了同样的异常.我怎么知道我应该设置什么读取限制?因为我将从输入流中读取不同的文件大小.
这是代码,但有错误:
bin = new ByteArrayInputStream(socket.getInputStream());
Run Code Online (Sandbox Code Playgroud)
是否可以从套接字接收byte[]使用ByteArrayInputStream?
是否可以在Android中的BluetoothSocket中实现inputstream.read()函数的超时?
我尝试过使用Thread.sleep(),但这只会暂停我的活动.
---更新---
我有一个想法,在这里编写2个线程代码(t1和t2),其中每个线程中断其他,其中一个(t1)进行休眠(5000)然后中断另一个线程(t2),从另一个线程中断另一个线程( t2)如果在读取时输入流检测到某个字符为0x0D中断另一个线程(t1),但这是我的问题,有人可以帮助我吗?因为我没有使用thread()方法的线程,希望有人能帮助我,谢谢...
---更新---
public void run(){
while(true){
try {
char r;
String respuesta = "";
while (true) {
r = (char) mmInStream.read();
respuesta += r;
if (r == 0x3e) {
break;
}
}
respuesta = respuesta.replaceAll(" ", "");
Log.d("respuesta", respuesta);
rHandler.obtainMessage(MESSAGE_READ, -1, -1, respuesta).sendToTarget();
} catch (IOException readException) {
Log.e("ServicioGeneral", "Error de lectura", readException);
this.interrupt();
connectionLost();
// posibly break();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的实现,当某些东西进入不同的线程时,问题是如果我没有从de mmInStream获取0x3e字符,将达到超时.
我想在第二个例子中我必须使用notifyAll(),但是,什么时候我必须启动readThread()?
谢谢,@ weeman
我在应用程序中使用Volley库.
在onresponse listener中我需要InputStream作为响应
我怎么得到它?
我有一个字符串:
var myString:String = "My String"
Run Code Online (Sandbox Code Playgroud)
如何InputStream在 Kotlin 中将其转换为?
inputstream ×10
java ×7
android ×3
arrays ×1
bluetooth ×1
filechannel ×1
filelock ×1
filepath ×1
httpclient ×1
io ×1
ioexception ×1
jar ×1
kotlin ×1
sockets ×1
string ×1
timeout ×1