Android Open Source - webimageloader Content U R L Connection






From Project

Back to project page webimageloader.

License

The source code is released under:

Apache License

If you think the Android project webimageloader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*-
 * Copyright (C) 2010 Google Inc.//from w w w. ja v a 2s .  co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.webimageloader.content;

import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;

/**
 * {@link URLConnection} implementation for {@code content://}, {@code file://},
 * and {@code android.resource://} URIs.
 */
class ContentURLConnection extends URLConnection {

    private final ContentResolver mResolver;

    private final Uri mUri;

    private InputStream mInputStream;

    private OutputStream mOutputStream;

    private boolean mConnected;

    private boolean mInputStreamClosed;

    private boolean mOutputStreamClosed;

    public ContentURLConnection(ContentResolver resolver, URL url) {
        super(url);
        mResolver = resolver;
        String spec = url.toString();
        mUri = Uri.parse(spec);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void connect() throws IOException {
        if (getDoInput()) {
            InputStream in = mResolver.openInputStream(mUri);
            mInputStream = new ContentURLConnectionInputStream(in);
        }
        if (getDoOutput()) {
            OutputStream out = mResolver.openOutputStream(mUri, "rwt");
            mOutputStream = new ContentURLConnectionOutputStream(out);
        }
        mConnected = true;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (mInputStreamClosed) {
            throw new IllegalStateException("Closed");
        }
        if (!mConnected) {
            connect();
        }
        return mInputStream;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        if (mOutputStreamClosed) {
            throw new IllegalStateException("Closed");
        }
        if (!mConnected) {
            connect();
        }
        return mOutputStream;
    }

    @Override
    public Object getContent() throws IOException {
        if (!mConnected) {
            connect();
        }
        return super.getContent();
    }

    @Override
    public String getContentType() {
        return mResolver.getType(mUri);
    }

    @Override
    public int getContentLength() {
        try {
            AssetFileDescriptor fd = mResolver.openAssetFileDescriptor(mUri, "r");
            long length = fd.getLength();
            if (length <= 0 && length <= Integer.MAX_VALUE) {
                return (int) length;
            }
        } catch (IOException e) {
        }
        return -1;
    }

    private class ContentURLConnectionInputStream extends FilterInputStream {

        public ContentURLConnectionInputStream(InputStream in) {
            super(in);
        }

        @Override
        public void close() throws IOException {
            super.close();
            mInputStreamClosed = true;
        }
    }

    private class ContentURLConnectionOutputStream extends FilterOutputStream {

        public ContentURLConnectionOutputStream(OutputStream out) {
            super(out);
        }

        @Override
        public void close() throws IOException {
            super.close();
            mOutputStreamClosed = true;
        }
    }
}




Java Source Code List

com.webimageloader.ConnectionFactory.java
com.webimageloader.ConnectionHandler.java
com.webimageloader.Constants.java
com.webimageloader.ImageLoaderImpl.java
com.webimageloader.ImageLoader.java
com.webimageloader.Request.java
com.webimageloader.content.ContentURLConnection.java
com.webimageloader.content.ContentURLStreamHandler.java
com.webimageloader.ext.ImageHelper.java
com.webimageloader.ext.ImageLoaderApplication.java
com.webimageloader.loader.BackgroundLoader.java
com.webimageloader.loader.DiskLoader.java
com.webimageloader.loader.LoaderManager.java
com.webimageloader.loader.LoaderRequest.java
com.webimageloader.loader.LoaderWork.java
com.webimageloader.loader.Loader.java
com.webimageloader.loader.MemoryCache.java
com.webimageloader.loader.MemoryLoader.java
com.webimageloader.loader.Metadata.java
com.webimageloader.loader.NetworkLoader.java
com.webimageloader.loader.PendingRequests.java
com.webimageloader.loader.SimpleBackgroundLoader.java
com.webimageloader.loader.TransformingLoader.java
com.webimageloader.sample.AsyncLoader.java
com.webimageloader.sample.ExampleApplication.java
com.webimageloader.sample.FastImageView.java
com.webimageloader.sample.MainActivity.java
com.webimageloader.sample.numbers.NumberDetailsActivity.java
com.webimageloader.sample.numbers.NumbersActivity.java
com.webimageloader.sample.patterns.PatternDetailsActivity.java
com.webimageloader.sample.patterns.PatternsActivity.java
com.webimageloader.sample.patterns.PatternsListFragment.java
com.webimageloader.sample.progress.ProgressActivity.java
com.webimageloader.transformation.ScaleTransformation.java
com.webimageloader.transformation.SimpleTransformation.java
com.webimageloader.transformation.Transformation.java
com.webimageloader.util.AbstractImageLoader.java
com.webimageloader.util.Android.java
com.webimageloader.util.BitmapUtils.java
com.webimageloader.util.FlushedInputStream.java
com.webimageloader.util.Hasher.java
com.webimageloader.util.HeaderParser.java
com.webimageloader.util.IOUtil.java
com.webimageloader.util.InputSupplier.java
com.webimageloader.util.ListenerFuture.java
com.webimageloader.util.LruCache.java
com.webimageloader.util.PriorityThreadFactory.java
com.webimageloader.util.WaitFuture.java