在Android-Java中查找输入流

在Android-Java中查找输入流,java,android,socket.io,inputstream,Java,Android,Socket.io,Inputstream,我有一个文件传输应用程序,通过套接字连接从Android向Windows发送大文件(大小为几GB)。我正在使用内容解析器将输入流实例获取到存储在手机中的文件中,但我希望能够在输入流上来回搜索,以便通过数据报通道更高效地传输文件。有办法吗 int ack, len; Context context = getApplicationContext(); ContentResolver cr = context.getContentResolver(); InputStream is = cr.op

我有一个文件传输应用程序,通过套接字连接从Android向Windows发送大文件(大小为几GB)。我正在使用内容解析器将输入流实例获取到存储在手机中的文件中,但我希望能够在输入流上来回搜索,以便通过数据报通道更高效地传输文件。有办法吗

int ack, len;

Context context = getApplicationContext();
ContentResolver cr = context.getContentResolver();
InputStream is = cr.openInputStream(fileUri);

while ((len = is.read(bufr, 0, BUFFER_SIZE)) > 0) {
        ack = sendDatagramPacket(bufr, 0, len);
}

这里有一个值得尝试的方法对我来说很有效。其思想是通过获取AssetFileDescriptor将InputStream强制转换为FileInputStream,然后获取FileChannel的一个实例,该实例允许您来回搜索

long position = 0, bytesRead = 0, currentRead = 0;

Context context = getApplicationContext();
ContentResolver cr = context.getContentResolver();
InputStream is = cr.openInputStream(fileUri);
AssetFileDescriptor assetFileDescriptor = null;
FileInputStream fis = null;
FileChannel fc = null;

        try {
            assetFileDescriptor = getContentResolver().openAssetFileDescriptor(fileUri[0], "r");
            is = cr.openInputStream(fileUri[0]);
            Utility.sendFileInfo(is, out, fileName);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        fis = new FileInputStream(assetFileDescriptor.getFileDescriptor());;
        fc = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(Utility.BUFFER_SIZE);

        while(position < filesize){
            try {
                fc.position(position);
                currentRead = fc.read(buffer);

                //Use the populated buffer to send data out
                SendDatagramPacket(buffer, 0, currentRead)
                bytesRead += currentRead;
                position = bytesRead;
                buffer = ByteBuffer.allocate(Utility.BUFFER_SIZE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
长位置=0,字节读取=0,当前读取=0;
Context=getApplicationContext();
ContentResolver cr=context.getContentResolver();
InputStream is=cr.openInputStream(fileUri);
AssetFileDescriptor AssetFileDescriptor=null;
FileInputStream fis=null;
FileChannel fc=null;
试一试{
assetFileDescriptor=getContentResolver();
is=cr.openInputStream(fileUri[0]);
sendFileInfo(is,out,fileName);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
fis=新的FileInputStream(assetFileDescriptor.getFileDescriptor());;
fc=fis.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(实用程序.缓冲区大小);
while(位置<文件大小){
试一试{
fc.职位(职位);
currentRead=fc.read(缓冲区);
//使用填充的缓冲区发送数据
SendDatagramPacket(缓冲区,0,currentRead)
字节读取+=当前读取;
位置=字节读取;
buffer=ByteBuffer.allocate(Utility.buffer\u SIZE);
}捕获(IOE异常){
e、 printStackTrace();
}
}

这里有一种方法值得尝试,对我来说很有效。其思想是通过获取AssetFileDescriptor将InputStream强制转换为FileInputStream,然后获取FileChannel的一个实例,该实例允许您来回搜索

long position = 0, bytesRead = 0, currentRead = 0;

Context context = getApplicationContext();
ContentResolver cr = context.getContentResolver();
InputStream is = cr.openInputStream(fileUri);
AssetFileDescriptor assetFileDescriptor = null;
FileInputStream fis = null;
FileChannel fc = null;

        try {
            assetFileDescriptor = getContentResolver().openAssetFileDescriptor(fileUri[0], "r");
            is = cr.openInputStream(fileUri[0]);
            Utility.sendFileInfo(is, out, fileName);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        fis = new FileInputStream(assetFileDescriptor.getFileDescriptor());;
        fc = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(Utility.BUFFER_SIZE);

        while(position < filesize){
            try {
                fc.position(position);
                currentRead = fc.read(buffer);

                //Use the populated buffer to send data out
                SendDatagramPacket(buffer, 0, currentRead)
                bytesRead += currentRead;
                position = bytesRead;
                buffer = ByteBuffer.allocate(Utility.BUFFER_SIZE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
长位置=0,字节读取=0,当前读取=0;
Context=getApplicationContext();
ContentResolver cr=context.getContentResolver();
InputStream is=cr.openInputStream(fileUri);
AssetFileDescriptor AssetFileDescriptor=null;
FileInputStream fis=null;
FileChannel fc=null;
试一试{
assetFileDescriptor=getContentResolver();
is=cr.openInputStream(fileUri[0]);
sendFileInfo(is,out,fileName);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
fis=新的FileInputStream(assetFileDescriptor.getFileDescriptor());;
fc=fis.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(实用程序.缓冲区大小);
while(位置<文件大小){
试一试{
fc.职位(职位);
currentRead=fc.read(缓冲区);
//使用填充的缓冲区发送数据
SendDatagramPacket(缓冲区,0,currentRead)
字节读取+=当前读取;
位置=字节读取;
buffer=ByteBuffer.allocate(Utility.buffer\u SIZE);
}捕获(IOE异常){
e、 printStackTrace();
}
}

Int len可能不等于缓冲区大小。因此,bufr可能无法完全填充。Try:
ack=sendDatagramPacket(bufr,0,len)谢谢,我写了这段代码来简化实际的代码。我现在包含了您的编辑。Int len可能不等于BUFFER_SIZE。因此,bufr可能无法完全填充。Try:
ack=sendDatagramPacket(bufr,0,len)谢谢,我写了这段代码来简化实际的代码。我现在包括了你的编辑。