AssetManager.java revision 69cb87576ba163b61bb0e6477a3b7c57a9b11d40
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.res;
18
19import android.os.ParcelFileDescriptor;
20import android.util.Config;
21import android.util.Log;
22import android.util.TypedValue;
23
24import java.io.FileNotFoundException;
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.HashMap;
28
29/**
30 * Provides access to an application's raw asset files; see {@link Resources}
31 * for the way most applications will want to retrieve their resource data.
32 * This class presents a lower-level API that allows you to open and read raw
33 * files that have been bundled with the application as a simple stream of
34 * bytes.
35 */
36public final class AssetManager {
37    /* modes used when opening an asset */
38
39    /**
40     * Mode for {@link #open(String, int)}: no specific information about how
41     * data will be accessed.
42     */
43    public static final int ACCESS_UNKNOWN = 0;
44    /**
45     * Mode for {@link #open(String, int)}: Read chunks, and seek forward and
46     * backward.
47     */
48    public static final int ACCESS_RANDOM = 1;
49    /**
50     * Mode for {@link #open(String, int)}: Read sequentially, with an
51     * occasional forward seek.
52     */
53    public static final int ACCESS_STREAMING = 2;
54    /**
55     * Mode for {@link #open(String, int)}: Attempt to load contents into
56     * memory, for fast small reads.
57     */
58    public static final int ACCESS_BUFFER = 3;
59
60    private static final String TAG = "AssetManager";
61    private static final boolean localLOGV = Config.LOGV || false;
62
63    private static final boolean DEBUG_REFS = false;
64
65    private static final Object sSync = new Object();
66    private static AssetManager sSystem = null;
67
68    private final TypedValue mValue = new TypedValue();
69    private final long[] mOffsets = new long[2];
70
71    // For communication with native code.
72    private int mObject;
73    private int mNObject;  // used by the NDK
74
75    private StringBlock mStringBlocks[] = null;
76
77    private int mNumRefs = 1;
78    private boolean mOpen = true;
79    private HashMap<Integer, RuntimeException> mRefStacks;
80
81    /**
82     * Create a new AssetManager containing only the basic system assets.
83     * Applications will not generally use this method, instead retrieving the
84     * appropriate asset manager with {@link Resources#getAssets}.    Not for
85     * use by applications.
86     * {@hide}
87     */
88    public AssetManager() {
89        synchronized (this) {
90            if (DEBUG_REFS) {
91                mNumRefs = 0;
92                incRefsLocked(this.hashCode());
93            }
94            init();
95            if (localLOGV) Log.v(TAG, "New asset manager: " + this);
96            ensureSystemAssets();
97        }
98    }
99
100    private static void ensureSystemAssets() {
101        synchronized (sSync) {
102            if (sSystem == null) {
103                AssetManager system = new AssetManager(true);
104                system.makeStringBlocks(false);
105                sSystem = system;
106            }
107        }
108    }
109
110    private AssetManager(boolean isSystem) {
111        if (DEBUG_REFS) {
112            synchronized (this) {
113                mNumRefs = 0;
114                incRefsLocked(this.hashCode());
115            }
116        }
117        init();
118        if (localLOGV) Log.v(TAG, "New asset manager: " + this);
119    }
120
121    /**
122     * Return a global shared asset manager that provides access to only
123     * system assets (no application assets).
124     * {@hide}
125     */
126    public static AssetManager getSystem() {
127        ensureSystemAssets();
128        return sSystem;
129    }
130
131    /**
132     * Close this asset manager.
133     */
134    public void close() {
135        synchronized(this) {
136            //System.out.println("Release: num=" + mNumRefs
137            //                   + ", released=" + mReleased);
138            if (mOpen) {
139                mOpen = false;
140                decRefsLocked(this.hashCode());
141            }
142        }
143    }
144
145    /**
146     * Retrieve the string value associated with a particular resource
147     * identifier for the current configuration / skin.
148     */
149    /*package*/ final CharSequence getResourceText(int ident) {
150        synchronized (this) {
151            TypedValue tmpValue = mValue;
152            int block = loadResourceValue(ident, (short) 0, tmpValue, true);
153            if (block >= 0) {
154                if (tmpValue.type == TypedValue.TYPE_STRING) {
155                    return mStringBlocks[block].get(tmpValue.data);
156                }
157                return tmpValue.coerceToString();
158            }
159        }
160        return null;
161    }
162
163    /**
164     * Retrieve the string value associated with a particular resource
165     * identifier for the current configuration / skin.
166     */
167    /*package*/ final CharSequence getResourceBagText(int ident, int bagEntryId) {
168        synchronized (this) {
169            TypedValue tmpValue = mValue;
170            int block = loadResourceBagValue(ident, bagEntryId, tmpValue, true);
171            if (block >= 0) {
172                if (tmpValue.type == TypedValue.TYPE_STRING) {
173                    return mStringBlocks[block].get(tmpValue.data);
174                }
175                return tmpValue.coerceToString();
176            }
177        }
178        return null;
179    }
180
181    /**
182     * Retrieve the string array associated with a particular resource
183     * identifier.
184     * @param id Resource id of the string array
185     */
186    /*package*/ final String[] getResourceStringArray(final int id) {
187        String[] retArray = getArrayStringResource(id);
188        return retArray;
189    }
190
191
192    /*package*/ final boolean getResourceValue(int ident,
193                                               int density,
194                                               TypedValue outValue,
195                                               boolean resolveRefs)
196    {
197        int block = loadResourceValue(ident, (short) density, outValue, resolveRefs);
198        if (block >= 0) {
199            if (outValue.type != TypedValue.TYPE_STRING) {
200                return true;
201            }
202            outValue.string = mStringBlocks[block].get(outValue.data);
203            return true;
204        }
205        return false;
206    }
207
208    /**
209     * Retrieve the text array associated with a particular resource
210     * identifier.
211     * @param id Resource id of the string array
212     */
213    /*package*/ final CharSequence[] getResourceTextArray(final int id) {
214        int[] rawInfoArray = getArrayStringInfo(id);
215        int rawInfoArrayLen = rawInfoArray.length;
216        final int infoArrayLen = rawInfoArrayLen / 2;
217        int block;
218        int index;
219        CharSequence[] retArray = new CharSequence[infoArrayLen];
220        for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) {
221            block = rawInfoArray[i];
222            index = rawInfoArray[i + 1];
223            retArray[j] = index >= 0 ? mStringBlocks[block].get(index) : null;
224        }
225        return retArray;
226    }
227
228    /*package*/ final boolean getThemeValue(int theme, int ident,
229            TypedValue outValue, boolean resolveRefs) {
230        int block = loadThemeAttributeValue(theme, ident, outValue, resolveRefs);
231        if (block >= 0) {
232            if (outValue.type != TypedValue.TYPE_STRING) {
233                return true;
234            }
235            StringBlock[] blocks = mStringBlocks;
236            if (blocks == null) {
237                ensureStringBlocks();
238            }
239            outValue.string = blocks[block].get(outValue.data);
240            return true;
241        }
242        return false;
243    }
244
245    /*package*/ final void ensureStringBlocks() {
246        if (mStringBlocks == null) {
247            synchronized (this) {
248                if (mStringBlocks == null) {
249                    makeStringBlocks(true);
250                }
251            }
252        }
253    }
254
255    private final void makeStringBlocks(boolean copyFromSystem) {
256        final int sysNum = copyFromSystem ? sSystem.mStringBlocks.length : 0;
257        final int num = getStringBlockCount();
258        mStringBlocks = new StringBlock[num];
259        if (localLOGV) Log.v(TAG, "Making string blocks for " + this
260                + ": " + num);
261        for (int i=0; i<num; i++) {
262            if (i < sysNum) {
263                mStringBlocks[i] = sSystem.mStringBlocks[i];
264            } else {
265                mStringBlocks[i] = new StringBlock(getNativeStringBlock(i), true);
266            }
267        }
268    }
269
270    /*package*/ final CharSequence getPooledString(int block, int id) {
271        //System.out.println("Get pooled: block=" + block
272        //                   + ", id=#" + Integer.toHexString(id)
273        //                   + ", blocks=" + mStringBlocks);
274        return mStringBlocks[block-1].get(id);
275    }
276
277    /**
278     * Open an asset using ACCESS_STREAMING mode.  This provides access to
279     * files that have been bundled with an application as assets -- that is,
280     * files placed in to the "assets" directory.
281     *
282     * @param fileName The name of the asset to open.  This name can be
283     *                 hierarchical.
284     *
285     * @see #open(String, int)
286     * @see #list
287     */
288    public final InputStream open(String fileName) throws IOException {
289        return open(fileName, ACCESS_STREAMING);
290    }
291
292    /**
293     * Open an asset using an explicit access mode, returning an InputStream to
294     * read its contents.  This provides access to files that have been bundled
295     * with an application as assets -- that is, files placed in to the
296     * "assets" directory.
297     *
298     * @param fileName The name of the asset to open.  This name can be
299     *                 hierarchical.
300     * @param accessMode Desired access mode for retrieving the data.
301     *
302     * @see #ACCESS_UNKNOWN
303     * @see #ACCESS_STREAMING
304     * @see #ACCESS_RANDOM
305     * @see #ACCESS_BUFFER
306     * @see #open(String)
307     * @see #list
308     */
309    public final InputStream open(String fileName, int accessMode)
310        throws IOException {
311        synchronized (this) {
312            if (!mOpen) {
313                throw new RuntimeException("Assetmanager has been closed");
314            }
315            int asset = openAsset(fileName, accessMode);
316            if (asset != 0) {
317                AssetInputStream res = new AssetInputStream(asset);
318                incRefsLocked(res.hashCode());
319                return res;
320            }
321        }
322        throw new FileNotFoundException("Asset file: " + fileName);
323    }
324
325    public final AssetFileDescriptor openFd(String fileName)
326            throws IOException {
327        synchronized (this) {
328            if (!mOpen) {
329                throw new RuntimeException("Assetmanager has been closed");
330            }
331            ParcelFileDescriptor pfd = openAssetFd(fileName, mOffsets);
332            if (pfd != null) {
333                return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
334            }
335        }
336        throw new FileNotFoundException("Asset file: " + fileName);
337    }
338
339    /**
340     * Return a String array of all the assets at the given path.
341     *
342     * @param path A relative path within the assets, i.e., "docs/home.html".
343     *
344     * @return String[] Array of strings, one for each asset.  These file
345     *         names are relative to 'path'.  You can open the file by
346     *         concatenating 'path' and a name in the returned string (via
347     *         File) and passing that to open().
348     *
349     * @see #open
350     */
351    public native final String[] list(String path)
352        throws IOException;
353
354    /**
355     * {@hide}
356     * Open a non-asset file as an asset using ACCESS_STREAMING mode.  This
357     * provides direct access to all of the files included in an application
358     * package (not only its assets).  Applications should not normally use
359     * this.
360     *
361     * @see #open(String)
362     */
363    public final InputStream openNonAsset(String fileName) throws IOException {
364        return openNonAsset(0, fileName, ACCESS_STREAMING);
365    }
366
367    /**
368     * {@hide}
369     * Open a non-asset file as an asset using a specific access mode.  This
370     * provides direct access to all of the files included in an application
371     * package (not only its assets).  Applications should not normally use
372     * this.
373     *
374     * @see #open(String, int)
375     */
376    public final InputStream openNonAsset(String fileName, int accessMode)
377        throws IOException {
378        return openNonAsset(0, fileName, accessMode);
379    }
380
381    /**
382     * {@hide}
383     * Open a non-asset in a specified package.  Not for use by applications.
384     *
385     * @param cookie Identifier of the package to be opened.
386     * @param fileName Name of the asset to retrieve.
387     */
388    public final InputStream openNonAsset(int cookie, String fileName)
389        throws IOException {
390        return openNonAsset(cookie, fileName, ACCESS_STREAMING);
391    }
392
393    /**
394     * {@hide}
395     * Open a non-asset in a specified package.  Not for use by applications.
396     *
397     * @param cookie Identifier of the package to be opened.
398     * @param fileName Name of the asset to retrieve.
399     * @param accessMode Desired access mode for retrieving the data.
400     */
401    public final InputStream openNonAsset(int cookie, String fileName, int accessMode)
402        throws IOException {
403        synchronized (this) {
404            if (!mOpen) {
405                throw new RuntimeException("Assetmanager has been closed");
406            }
407            int asset = openNonAssetNative(cookie, fileName, accessMode);
408            if (asset != 0) {
409                AssetInputStream res = new AssetInputStream(asset);
410                incRefsLocked(res.hashCode());
411                return res;
412            }
413        }
414        throw new FileNotFoundException("Asset absolute file: " + fileName);
415    }
416
417    public final AssetFileDescriptor openNonAssetFd(String fileName)
418            throws IOException {
419        return openNonAssetFd(0, fileName);
420    }
421
422    public final AssetFileDescriptor openNonAssetFd(int cookie,
423            String fileName) throws IOException {
424        synchronized (this) {
425            if (!mOpen) {
426                throw new RuntimeException("Assetmanager has been closed");
427            }
428            ParcelFileDescriptor pfd = openNonAssetFdNative(cookie,
429                    fileName, mOffsets);
430            if (pfd != null) {
431                return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
432            }
433        }
434        throw new FileNotFoundException("Asset absolute file: " + fileName);
435    }
436
437    /**
438     * Retrieve a parser for a compiled XML file.
439     *
440     * @param fileName The name of the file to retrieve.
441     */
442    public final XmlResourceParser openXmlResourceParser(String fileName)
443            throws IOException {
444        return openXmlResourceParser(0, fileName);
445    }
446
447    /**
448     * Retrieve a parser for a compiled XML file.
449     *
450     * @param cookie Identifier of the package to be opened.
451     * @param fileName The name of the file to retrieve.
452     */
453    public final XmlResourceParser openXmlResourceParser(int cookie,
454            String fileName) throws IOException {
455        XmlBlock block = openXmlBlockAsset(cookie, fileName);
456        XmlResourceParser rp = block.newParser();
457        block.close();
458        return rp;
459    }
460
461    /**
462     * {@hide}
463     * Retrieve a non-asset as a compiled XML file.  Not for use by
464     * applications.
465     *
466     * @param fileName The name of the file to retrieve.
467     */
468    /*package*/ final XmlBlock openXmlBlockAsset(String fileName)
469            throws IOException {
470        return openXmlBlockAsset(0, fileName);
471    }
472
473    /**
474     * {@hide}
475     * Retrieve a non-asset as a compiled XML file.  Not for use by
476     * applications.
477     *
478     * @param cookie Identifier of the package to be opened.
479     * @param fileName Name of the asset to retrieve.
480     */
481    /*package*/ final XmlBlock openXmlBlockAsset(int cookie, String fileName)
482        throws IOException {
483        synchronized (this) {
484            if (!mOpen) {
485                throw new RuntimeException("Assetmanager has been closed");
486            }
487            int xmlBlock = openXmlAssetNative(cookie, fileName);
488            if (xmlBlock != 0) {
489                XmlBlock res = new XmlBlock(this, xmlBlock);
490                incRefsLocked(res.hashCode());
491                return res;
492            }
493        }
494        throw new FileNotFoundException("Asset XML file: " + fileName);
495    }
496
497    /*package*/ void xmlBlockGone(int id) {
498        synchronized (this) {
499            decRefsLocked(id);
500        }
501    }
502
503    /*package*/ final int createTheme() {
504        synchronized (this) {
505            if (!mOpen) {
506                throw new RuntimeException("Assetmanager has been closed");
507            }
508            int res = newTheme();
509            incRefsLocked(res);
510            return res;
511        }
512    }
513
514    /*package*/ final void releaseTheme(int theme) {
515        synchronized (this) {
516            deleteTheme(theme);
517            decRefsLocked(theme);
518        }
519    }
520
521    protected void finalize() throws Throwable {
522        try {
523            if (DEBUG_REFS && mNumRefs != 0) {
524                Log.w(TAG, "AssetManager " + this
525                        + " finalized with non-zero refs: " + mNumRefs);
526                if (mRefStacks != null) {
527                    for (RuntimeException e : mRefStacks.values()) {
528                        Log.w(TAG, "Reference from here", e);
529                    }
530                }
531            }
532            destroy();
533        } finally {
534            super.finalize();
535        }
536    }
537
538    public final class AssetInputStream extends InputStream {
539        public final int getAssetInt() {
540            return mAsset;
541        }
542        private AssetInputStream(int asset)
543        {
544            mAsset = asset;
545            mLength = getAssetLength(asset);
546        }
547        public final int read() throws IOException {
548            return readAssetChar(mAsset);
549        }
550        public final boolean markSupported() {
551            return true;
552        }
553        public final int available() throws IOException {
554            long len = getAssetRemainingLength(mAsset);
555            return len > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)len;
556        }
557        public final void close() throws IOException {
558            synchronized (AssetManager.this) {
559                if (mAsset != 0) {
560                    destroyAsset(mAsset);
561                    mAsset = 0;
562                    decRefsLocked(hashCode());
563                }
564            }
565        }
566        public final void mark(int readlimit) {
567            mMarkPos = seekAsset(mAsset, 0, 0);
568        }
569        public final void reset() throws IOException {
570            seekAsset(mAsset, mMarkPos, -1);
571        }
572        public final int read(byte[] b) throws IOException {
573            return readAsset(mAsset, b, 0, b.length);
574        }
575        public final int read(byte[] b, int off, int len) throws IOException {
576            return readAsset(mAsset, b, off, len);
577        }
578        public final long skip(long n) throws IOException {
579            long pos = seekAsset(mAsset, 0, 0);
580            if ((pos+n) > mLength) {
581                n = mLength-pos;
582            }
583            if (n > 0) {
584                seekAsset(mAsset, n, 0);
585            }
586            return n;
587        }
588
589        protected void finalize() throws Throwable
590        {
591            close();
592        }
593
594        private int mAsset;
595        private long mLength;
596        private long mMarkPos;
597    }
598
599    /**
600     * Add an additional set of assets to the asset manager.  This can be
601     * either a directory or ZIP file.  Not for use by applications.  Returns
602     * the cookie of the added asset, or 0 on failure.
603     * {@hide}
604     */
605    public native final int addAssetPath(String path);
606
607    /**
608     * Add multiple sets of assets to the asset manager at once.  See
609     * {@link #addAssetPath(String)} for more information.  Returns array of
610     * cookies for each added asset with 0 indicating failure, or null if
611     * the input array of paths is null.
612     * {@hide}
613     */
614    public final int[] addAssetPaths(String[] paths) {
615        if (paths == null) {
616            return null;
617        }
618
619        int[] cookies = new int[paths.length];
620        for (int i = 0; i < paths.length; i++) {
621            cookies[i] = addAssetPath(paths[i]);
622        }
623
624        return cookies;
625    }
626
627    /**
628     * Determine whether the state in this asset manager is up-to-date with
629     * the files on the filesystem.  If false is returned, you need to
630     * instantiate a new AssetManager class to see the new data.
631     * {@hide}
632     */
633    public native final boolean isUpToDate();
634
635    /**
636     * Change the locale being used by this asset manager.  Not for use by
637     * applications.
638     * {@hide}
639     */
640    public native final void setLocale(String locale);
641
642    /**
643     * Get the locales that this asset manager contains data for.
644     */
645    public native final String[] getLocales();
646
647    /**
648     * Change the configuation used when retrieving resources.  Not for use by
649     * applications.
650     * {@hide}
651     */
652    public native final void setConfiguration(int mcc, int mnc, String locale,
653            int orientation, int touchscreen, int density, int keyboard,
654            int keyboardHidden, int navigation, int screenWidth, int screenHeight,
655            int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp,
656            int screenLayout, int uiMode, int majorVersion);
657
658    /**
659     * Retrieve the resource identifier for the given resource name.
660     */
661    /*package*/ native final int getResourceIdentifier(String type,
662                                                       String name,
663                                                       String defPackage);
664
665    /*package*/ native final String getResourceName(int resid);
666    /*package*/ native final String getResourcePackageName(int resid);
667    /*package*/ native final String getResourceTypeName(int resid);
668    /*package*/ native final String getResourceEntryName(int resid);
669
670    private native final int openAsset(String fileName, int accessMode);
671    private final native ParcelFileDescriptor openAssetFd(String fileName,
672            long[] outOffsets) throws IOException;
673    private native final int openNonAssetNative(int cookie, String fileName,
674            int accessMode);
675    private native ParcelFileDescriptor openNonAssetFdNative(int cookie,
676            String fileName, long[] outOffsets) throws IOException;
677    private native final void destroyAsset(int asset);
678    private native final int readAssetChar(int asset);
679    private native final int readAsset(int asset, byte[] b, int off, int len);
680    private native final long seekAsset(int asset, long offset, int whence);
681    private native final long getAssetLength(int asset);
682    private native final long getAssetRemainingLength(int asset);
683
684    /** Returns true if the resource was found, filling in mRetStringBlock and
685     *  mRetData. */
686    private native final int loadResourceValue(int ident, short density, TypedValue outValue,
687            boolean resolve);
688    /** Returns true if the resource was found, filling in mRetStringBlock and
689     *  mRetData. */
690    private native final int loadResourceBagValue(int ident, int bagEntryId, TypedValue outValue,
691                                               boolean resolve);
692    /*package*/ static final int STYLE_NUM_ENTRIES = 6;
693    /*package*/ static final int STYLE_TYPE = 0;
694    /*package*/ static final int STYLE_DATA = 1;
695    /*package*/ static final int STYLE_ASSET_COOKIE = 2;
696    /*package*/ static final int STYLE_RESOURCE_ID = 3;
697    /*package*/ static final int STYLE_CHANGING_CONFIGURATIONS = 4;
698    /*package*/ static final int STYLE_DENSITY = 5;
699    /*package*/ native static final boolean applyStyle(int theme,
700            int defStyleAttr, int defStyleRes, int xmlParser,
701            int[] inAttrs, int[] outValues, int[] outIndices);
702    /*package*/ native final boolean retrieveAttributes(
703            int xmlParser, int[] inAttrs, int[] outValues, int[] outIndices);
704    /*package*/ native final int getArraySize(int resource);
705    /*package*/ native final int retrieveArray(int resource, int[] outValues);
706    private native final int getStringBlockCount();
707    private native final int getNativeStringBlock(int block);
708
709    /**
710     * {@hide}
711     */
712    public native final String getCookieName(int cookie);
713
714    /**
715     * {@hide}
716     */
717    public native static final int getGlobalAssetCount();
718
719    /**
720     * {@hide}
721     */
722    public native static final String getAssetAllocations();
723
724    /**
725     * {@hide}
726     */
727    public native static final int getGlobalAssetManagerCount();
728
729    private native final int newTheme();
730    private native final void deleteTheme(int theme);
731    /*package*/ native static final void applyThemeStyle(int theme, int styleRes, boolean force);
732    /*package*/ native static final void copyTheme(int dest, int source);
733    /*package*/ native static final int loadThemeAttributeValue(int theme, int ident,
734                                                                TypedValue outValue,
735                                                                boolean resolve);
736    /*package*/ native static final void dumpTheme(int theme, int priority, String tag, String prefix);
737
738    private native final int openXmlAssetNative(int cookie, String fileName);
739
740    private native final String[] getArrayStringResource(int arrayRes);
741    private native final int[] getArrayStringInfo(int arrayRes);
742    /*package*/ native final int[] getArrayIntResource(int arrayRes);
743
744    private native final void init();
745    private native final void destroy();
746
747    private final void incRefsLocked(int id) {
748        if (DEBUG_REFS) {
749            if (mRefStacks == null) {
750                mRefStacks = new HashMap<Integer, RuntimeException>();
751                RuntimeException ex = new RuntimeException();
752                ex.fillInStackTrace();
753                mRefStacks.put(this.hashCode(), ex);
754            }
755        }
756        mNumRefs++;
757    }
758
759    private final void decRefsLocked(int id) {
760        if (DEBUG_REFS && mRefStacks != null) {
761            mRefStacks.remove(id);
762        }
763        mNumRefs--;
764        //System.out.println("Dec streams: mNumRefs=" + mNumRefs
765        //                   + " mReleased=" + mReleased);
766        if (mNumRefs == 0) {
767            destroy();
768        }
769    }
770}
771