Example usage for android.os ParcelFileDescriptor hashCode

List of usage examples for android.os ParcelFileDescriptor hashCode

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:org.opensilk.music.cast.CastWebServer.java

/**
 * Fetches and serves the album art/*from   w w w  . ja  va2 s  .com*/
 *
 * @param uri
 * @param headers
 * @return
 */
//@DebugLog
private Response serveArt(Map<String, String> headers, Map<String, String> params, String uri) {
    String artist = params.get("artist");
    String album = params.get("album");
    if (TextUtils.isEmpty(artist) || TextUtils.isEmpty(album)) {
        return notFoundResponse();
    }
    String reqEtag = headers.get("if-none-match");
    if (!quiet)
        Log.d(TAG, "requested Art etag " + reqEtag);
    // Check our cache if we served up this etag for this url already
    // we can just return and save ourselfs a lot of expensive db/disk queries
    if (!TextUtils.isEmpty(reqEtag)) {
        String oldUri;
        synchronized (mEtagCache) {
            oldUri = mEtagCache.get(reqEtag);
        }
        if (oldUri != null && oldUri.equals(uri)) {
            // We already served it
            return createResponse(Response.Status.NOT_MODIFIED, MIME_ART, "");
        }
    }
    // We've got get get the art
    InputStream parcelIn = null;
    ByteArrayOutputStream tmpOut = null;
    try {
        final ParcelFileDescriptor pfd = mContext.getContentResolver()
                .openFileDescriptor(ArtworkProvider.createArtworkUri(artist, album), "r");
        //Hackish but hopefully will yield unique etags (at least for this session)
        String etag = Integer.toHexString(pfd.hashCode());
        if (!quiet)
            Log.d(TAG, "Created etag " + etag + " for " + uri);
        synchronized (mEtagCache) {
            mEtagCache.put(etag, uri);
        }
        // pipes dont perform well over the network and tend to get broken
        // so copy the image into memory and send the copy
        parcelIn = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
        tmpOut = new PoolingByteArrayOutputStream(mBytePool, 512 * 1024);
        IOUtils.copy(parcelIn, tmpOut);
        if (!quiet)
            Log.d(TAG, "image size=" + tmpOut.size() / 1024.0 + "k");
        Response res = createResponse(Response.Status.OK, MIME_ART,
                new ByteArrayInputStream(tmpOut.toByteArray()));
        res.addHeader("ETag", etag);
        return res;
    } catch (NullPointerException | IOException e) {
        // Serve up the default art
        return createResponse(Response.Status.OK, MIME_ART,
                mContext.getResources().openRawResource(R.drawable.default_artwork));
    } finally {
        IOUtils.closeQuietly(tmpOut);
        IOUtils.closeQuietly(parcelIn);
    }
}