001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils;
038:
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.util.HashMap;
042: import java.util.Locale;
043: import java.util.Map;
044: import java.util.ResourceBundle;
045:
046: /**
047: *
048: * @author Kirill Sorokin
049: */
050: public final class ResourceUtils {
051: /////////////////////////////////////////////////////////////////////////////////
052: // Static
053: private static Map<String, ResourceBundle> loadedBundles = new HashMap<String, ResourceBundle>();
054:
055: // strings //////////////////////////////////////////////////////////////////////
056: public static String getString(final String baseName,
057: final String key) {
058: return loadBundle(baseName, Locale.getDefault(),
059: ResourceUtils.class.getClassLoader()).getString(key);
060: }
061:
062: public static String getString(final String baseName,
063: final String key, final Object... arguments) {
064: return StringUtils.format(getString(baseName, key), arguments);
065: }
066:
067: public static String getString(final String baseName,
068: final String key, final ClassLoader loader) {
069: return loadBundle(baseName, Locale.getDefault(), loader)
070: .getString(key);
071: }
072:
073: public static String getString(final String baseName,
074: final String key, final ClassLoader loader,
075: final Object... arguments) {
076: return StringUtils.format(getString(baseName, key, loader),
077: arguments);
078: }
079:
080: public static String getString(final Class clazz, final String key) {
081: return loadBundle(clazz, Locale.getDefault()).getString(key);
082: }
083:
084: public static String getString(final Class clazz, final String key,
085: final Object... arguments) {
086: return StringUtils.format(getString(clazz, key), arguments);
087: }
088:
089: // resources ////////////////////////////////////////////////////////////////////
090: public static InputStream getResource(final String name) {
091: return getResource(name, ResourceUtils.class.getClassLoader());
092: }
093:
094: public static InputStream getResource(final String path,
095: final ClassLoader loader) {
096: return loader.getResourceAsStream(path);
097: }
098:
099: /**
100: * Returns the size of the resource file.
101: * @param resource Resource name
102: * @return size of the resource or
103: * <i>-1</i> if the resource was not found or any other error occured
104: */
105: public static long getResourceSize(final String resource) {
106: InputStream is = null;
107: long size = 0;
108: try {
109: is = getResource(resource);
110: if (is == null) { // resource was not found
111: return -1;
112: }
113: byte[] buf = new byte[BUFFER_SIZE];
114: while (is.available() > 0) {
115: size += is.read(buf);
116: }
117: } catch (IOException ex) {
118: size = -1;
119: } finally {
120: try {
121: if (is != null) {
122: is.close();
123: }
124: } catch (IOException e) {
125: }
126: }
127: return size;
128: }
129:
130: public static String getResourceFileName(final String resource) {
131: return resource.substring(resource.lastIndexOf("/") + 1);
132: }
133:
134: // private //////////////////////////////////////////////////////////////////////
135: private static ResourceBundle loadBundle(final String baseName,
136: final Locale locale, final ClassLoader loader) {
137: final String bundleId = loader.toString() + baseName;
138:
139: ResourceBundle bundle = (ResourceBundle) loadedBundles
140: .get(bundleId);
141:
142: if (bundle == null) {
143: bundle = ResourceBundle.getBundle(baseName, locale, loader);
144: loadedBundles.put(bundleId, bundle);
145: }
146:
147: return bundle;
148: }
149:
150: private static ResourceBundle loadBundle(final Class clazz,
151: final Locale locale) {
152: return loadBundle(clazz.getPackage().getName()
153: + BUNDLE_FILE_SUFFIX, locale, clazz.getClassLoader());
154: }
155:
156: /////////////////////////////////////////////////////////////////////////////////
157: // Instance
158: private ResourceUtils() {
159: // does nothing
160: }
161:
162: /////////////////////////////////////////////////////////////////////////////////
163: // Constants
164: public static final int BUFFER_SIZE = 40960; // NOMAGI
165:
166: public static final String BUNDLE_FILE_SUFFIX = ".Bundle"; // NOI18N
167: }
|