简体   繁体   中英

Converting InputStream to MappedByteBuffer in java?

I'm trying to create a android app.

I use InputStream inputStream=getAssets().open("book.txt"); to read the book.txt ;

I just want get the MappedByteBuffer object from InputStream ;

who konws?

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

I use 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


You can't do this with an InputStream . You should instead get a FileInputStream for your book.txt or cast your inputStream and then get a FileChannel from it:

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. 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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