简体   繁体   English

在Java中将InputStream转换为MappedByteBuffer?

[英]Converting InputStream to MappedByteBuffer in java?

I'm trying to create a android app. 我正在尝试创建一个Android应用。

I use InputStream inputStream=getAssets().open("book.txt"); 我使用InputStream inputStream=getAssets().open("book.txt"); to read the book.txt ; 阅读book.txt ;

I just want get the MappedByteBuffer object from InputStream ; 我只想从InputStream获取MappedByteBuffer对象;

who konws? 谁知道?

========= =========== ====================

I use FileInputStream fis = (FileInputStream) inputStream; 我使用FileInputStream fis = (FileInputStream) inputStream; cause an error! 引起错误! here is the error log: 这是错误日志:

10-27 10:45:55.830: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 22K, 53% free 2577K/5379K, external 1916K/2428K, paused 62ms
10-27 10:45:55.940: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 2K, 53% free 2577K/5379K, external 2516K/2516K, paused 69ms
10-27 10:45:56.230: I/fileName(9243): sahala.txt
10-27 10:45:56.240: D/szipinf(9243): Initializing inflate state
10-27 10:45:56.240: D/AndroidRuntime(9243): Shutting down VM
10-27 10:45:56.240: W/dalvikvm(9243): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-27 10:45:56.260: E/AndroidRuntime(9243): FATAL EXCEPTION: main
10-27 10:45:56.260: E/AndroidRuntime(9243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silenceper.bookdemo/com.silenceper.book.shl.activity.ReadBookActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.os.Looper.loop(Looper.java:123)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at java.lang.reflect.Method.invoke(Method.java:507)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at dalvik.system.NativeStart.main(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243): Caused by: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.utils.BookPageFactory.openBookFromInputStream(BookPageFactory.java:83)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.activity.ReadBookActivity.initBookData(ReadBookActivity.java:71)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at com.silenceper.book.shl.activity.ReadBookActivity.onCreate(ReadBookActivity.java:58)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-27 10:45:56.260: E/AndroidRuntime(9243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-27 10:45:56.260: E/AndroidRuntime(9243):     ... 11 more

Updated 更新

To do this you need to first copy your asset to a file and then apply the technique below to a file. 为此,您需要先将资产复制到文件,然后将以下技术应用于文件。 If your asset is more than 1MB you will require to split it - Load files bigger than 1M from assets folder 如果您的资产大于1MB,则需要拆分- 从资产文件夹中加载大于1M的文件


You can't do this with an InputStream . 您不能使用InputStream做到这一点。 You should instead get a FileInputStream for your book.txt or cast your inputStream and then get a FileChannel from it: 相反,您应该为book.txt获取FileInputStreambook.txt inputStream ,然后从中获取FileChannel

try {
    FileInputStream fis = (FileInputStream) inputStream;
    FileChannel channel = fis.getChannel();
    ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    byte[] bytes = new byte[102];
    buffer.get(bytes);
    System.out.println(new String(bytes));
} catch (IOException e) {
    e.printStackTrace();
}

I had the similar need of doing this. 我也有这样做的需要。 Here is what I did: 这是我所做的:
1) Open resource file with asset manager and write a regular file to /data/data/com.yourpackage/files directory. 1)使用Asset Manager打开资源文件,然后将常规文件写入/data/data/com.yourpackage/files目录。 Sample code: 样例代码:

private void prepareModelFile() throws IOException {
        AssetManager assetManager = getAssets();
        String configDir = getFilesDir().getAbsolutePath();
        InputStream stream = assetManager.open("mytestfile_in_assets.data");
        mTFLiteModelFile = configDir +"/mytestfilename.data";
            OutputStream output = new BufferedOutputStream(new FileOutputStream(mTFLiteModelFile));
            copyFile(stream, output);
    }

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();;
    out.close();
}

2) Now you have the file copied out of assets, you can use the following way to create a MappedByteBuffer. 2)现在,您已经从资产中复制了文件,可以使用以下方式创建MappedByteBuffer。 Example: 例:

        FileInputStream inputStream = new FileInputStream(mTFLiteModelFile);
        FileChannel fileChannel = inputStream.getChannel();
        MappedByteBuffer myMappedBuffer =  fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

Hope this helps. 希望这可以帮助。

David 大卫

由于许多原因,您无法执行此操作,其中大多数都暗示该问题没有道理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM