alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.execution;

import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownServiceException;
import java.security.Permission;

import org.openide.filesystems.*;

/** Special URL connection directly accessing an internal file object.
*
* @author Ales Novak, Petr Hamernik, Jan Jancura, Jaroslav Tulach
 * @deprecated Use {@link URLMapper} instead.
*/
public final class NbfsURLConnection extends URLConnection {
    /** Protocol name for this type of URL. */
    public static final String PROTOCOL = "nbfs"; // NOI18N

    /** Connection created by the fileobject that we want to delegate to */
    private URLConnection del;


    /**
    * Create a new connection to a {@link FileObject}.
    * @param u URL of the connection. Please use {@link #encodeFileObject(FileObject)} to create the URL.
    */
    public NbfsURLConnection (URL u) {
        super (u);
    }


    /** Provides a URL to access a file object.
    * @param fo the file object
    * @return a URL using the correct syntax and {@link #PROTOCOL protocol}
    * @exception FileStateInvalidException if the file object is not valid (typically, if its filesystem is inconsistent or no longer present)
    */
    public static URL encodeFileObject (FileObject fo) throws FileStateInvalidException {
        return fo.getURL ();
    }

    /** Retrieves the file object specified by an internal URL.
    * @param u the url to decode
    * @return the file object that is represented by the URL, or null if the URL is somehow invalid or the file does not exist
    */
    public static FileObject decodeURL (URL u) {
        if (!u.getProtocol ().equals (PROTOCOL)) return null;
        
        FileObject[] fos = URLMapper.findFileObjects(u);
        return (fos.length > 0) ? fos [0] : null;
    }


    /* A method for connecting to a FileObject.
    */
    public void connect() throws IOException {
        if (del != null) return;
        
        synchronized (this) {
            if (del != null) return;
            
            FileObject fo = decodeURL (url);
            if (fo == null) {
                throw new IOException("Cannot find: " + url); // NOI18N
            }
            del = fo.getURL ().openConnection ();
        }
    }

    /*
    * @return InputStream or given FileObject.
    */
    public InputStream getInputStream()
    throws IOException, UnknownServiceException {
        connect ();
        
        return del.getInputStream ();
    }

    /*
    * @return OutputStream for given FileObject.
    */
    public OutputStream getOutputStream()
    throws IOException, UnknownServiceException {
        connect();
        
        return del.getOutputStream ();
    }

    /*
    * @return length of FileObject.
    */
    public int getContentLength() {
        try {
            connect();
            return del.getContentLength ();
        } catch (IOException ex) {
            return 0;
        }
    }


    /** Get a header field (currently, content type only).
    * @param name the header name. Only content-type is guaranteed to be present.
    * @return the value (i.e., MIME type)
    */
    public String getHeaderField(String name) {
        try {
            connect ();
            return del.getHeaderField (name);
        } catch (IOException ex) {
            return super.getHeaderField (name);
        }
    }


    // #13038: delegate to FileURL
    public Permission getPermission() throws IOException {
        if (del != null) {
            return del.getPermission();
        } else {
            // Just a guess.
            return new FilePermission("<>", "read"); // NOI18N
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.