Replace stream wrap-function w/ more specific ones

The current stream wrapper returns a potentially incorrect
value for a call to getLength(), is typically copied into
another stream (not always in the same way), and doesn't
always take advantage of its underlying data (like when it
is an Asset). The overall goal of this CL is to provide the
caller with something that is ready to use, depending on
what is asked for. If a copy is desired, the copy is made
before being returned to the caller.

core/jni/android/graphics/Bitmap.cpp:

    Include SkStream.h, since it is no longer included by
    CreateJavaOutputStreamAdaptor's header file.

core/jni/android/graphics/BitmapFactory.cpp:

    Pass an SkStreamRewindable to decoding functions, as Skia
    decoders will be updated to only take an SkStreamRewindable
    (which makes more sense because they require rewinding).

    Call the more specific GetRewindableStream to get a
    rewindable stream.

    Remove copyAssetToStream which has been moved to Utils.

    In nativeDecodeAsset, pass forcePurgeable as allowPurgeable
    in doDecode. Technically the old code worked, but it checked
    the BitmapOptions again.

    Remove getFDSize, which is no longer used.

core/jni/android/graphics/BitmapRegionDecoder.cpp:

    Remove redundant buildSkMemoryStream. nativeNewInstanceFromStream
    now calls CopyJavaInputStream, which handles the copy.

    Copy the Asset directly, using common code, rather than creating
    an AssetStreamAdaptor to copy.

core/jni/android/graphics/CreateJavaOutputStreamAdaptor.cpp:
core/jni/android/graphics/CreateJavaOutputStreamAdaptor.h:

    Provide new interfaces to access data from a Java InputStream.
    The new interfaces are more specific about what type of stream
    is desired.

    Use forward declarations where possible.

    Remove doSize, which gives a misleading answer to the question
    of how long the entire stream is.

    TODO: Only call FindClass etc once.

core/jni/android/graphics/Movie.cpp:

    Check for an asset stream, and use it if possible. Then call
    GetRewindableStream if there is not an asset.
    Remove the memory leak. Call DeleteLocalRef to delete the
    allocated memory.

core/jni/android/graphics/Picture.cpp:

    Call the new interface.

core/jni/android/graphics/Utils.cpp:
core/jni/android/graphics/Utils.h:

    Make AssetStreamAdaptor inherit from SkStreamRewindable so it
    can be passed to Skia decoding functions once they require it.

    Add CopyAssetToStream (moved from BitmapFactory.cpp) so it can
    be used by multiple files.

graphics/java/android/graphics/BitmapFactory.java:

    Remove the call to mark, which is now done natively.

    Remove the BufferedInputStream. Mark/reset is now handled
    by native code.

    Allow decodeStream to handle a FileInputStream by using the
    FileDescriptor, if it is seekable. In decodeFileDescriptor,
    call nativeDecodeStream instead of decodeStream so this new
    functionality will not loop.

    Call setDensityFromOptions in decodeFileDescriptor.

graphics/java/android/graphics/BitmapRegionDecoder.java:

    Remove the BufferedInputStream. Mark/reset is now handled
    by native code.

TODO: ADD TESTS!

Requires https://googleplex-android-review.googlesource.com/#/c/344317/

BUG=https://b.corp.google.com/issue?id=8432093

Change-Id: I4419b70b3482325c98ecc673dbfc4613f1b18581
diff --git a/core/jni/android/graphics/Utils.cpp b/core/jni/android/graphics/Utils.cpp
index cf6977e..b7d1f3a 100644
--- a/core/jni/android/graphics/Utils.cpp
+++ b/core/jni/android/graphics/Utils.cpp
@@ -28,12 +28,28 @@
     return true;
 }
 
+size_t AssetStreamAdaptor::getLength() const {
+    return fAsset->getLength();
+}
+
+bool AssetStreamAdaptor::isAtEnd() const {
+    return fAsset->getRemainingLength() == 0;
+}
+
+SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
+    SkASSERT(false);
+    // Cannot create a duplicate, since each AssetStreamAdaptor
+    // would be modifying the Asset.
+    //return new AssetStreamAdaptor(fAsset);
+    return NULL;
+}
+
 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
     ssize_t amount;
 
     if (NULL == buffer) {
-        if (0 == size) {  // caller is asking us for our total length
-            return fAsset->getLength();
+        if (0 == size) {
+            return 0;
         }
         // asset->seek returns new total offset
         // we want to return amount that was skipped
@@ -62,6 +78,34 @@
     return amount;
 }
 
+SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
+    if (NULL == asset) {
+        return NULL;
+    }
+
+    off64_t size = asset->seek(0, SEEK_SET);
+    if ((off64_t)-1 == size) {
+        SkDebugf("---- copyAsset: asset rewind failed\n");
+        return NULL;
+    }
+
+    size = asset->getLength();
+    if (size <= 0) {
+        SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
+        return NULL;
+    }
+
+    SkMemoryStream* stream = new SkMemoryStream(size);
+    void* data = const_cast<void*>(stream->getMemoryBase());
+    off64_t len = asset->read(data, size);
+    if (len != size) {
+        SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
+        delete stream;
+        stream = NULL;
+    }
+    return stream;
+}
+
 jobject android::nullObjectReturn(const char msg[]) {
     if (msg) {
         SkDebugf("--- %s\n", msg);