001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.util;
031:
032: import java.io.ByteArrayOutputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035:
036: /**
037: * Provides functionality for obtaining text and binary resource files.
038: */
039: public class Resource {
040:
041: private static final int BUFFER_SIZE = 4096;
042:
043: /**
044: * A RuntimeException exception that will be thrown in the event that
045: * problems are encountered obtaining a resource.
046: */
047: public static class ResourceException extends RuntimeException {
048:
049: /**
050: * Creates a resource exception.
051: *
052: * @param description A description of the error.
053: */
054: private ResourceException(String description) {
055: super (description);
056: }
057: }
058:
059: /**
060: * Retrieves the specified resource as a <code>String</code>.
061: *
062: * @param resourceName The name of the resource to be retrieved.
063: * @return The specified resource as a <code>String</code>.
064: */
065: public static String getResourceAsString(String resourceName) {
066: return getResource(resourceName).toString();
067: }
068:
069: /**
070: * Retrieves the specified resource as an array of <code>byte</code>s.
071: *
072: * @param resourceName The name of the resource to be retrieved.
073: * @return The specified resource as an array of <code>byte<code>s.
074: */
075: public static byte[] getResourceAsByteArray(String resourceName) {
076: return getResource(resourceName).toByteArray();
077: }
078:
079: /**
080: * An internal method used to retrieve a resource as a
081: * <code>ByteArrayOutputStream</code>.
082: *
083: * @param resourceName The name of the resource to be retrieved.
084: * @return A <code>ByteArrayOutputStream</code> of the content of the
085: * resource.
086: */
087: private static ByteArrayOutputStream getResource(String resourceName) {
088: InputStream in = null;
089: byte[] buffer = new byte[BUFFER_SIZE];
090: ByteArrayOutputStream out = null;
091: int bytesRead = 0;
092:
093: try {
094: in = Resource.class.getResourceAsStream(resourceName);
095: if (in == null) {
096: throw new ResourceException(
097: "Resource does not exist: \"" + resourceName
098: + "\"");
099: }
100: out = new ByteArrayOutputStream();
101: do {
102: bytesRead = in.read(buffer);
103: if (bytesRead > 0) {
104: out.write(buffer, 0, bytesRead);
105: }
106: } while (bytesRead > 0);
107: } catch (IOException ex) {
108: throw new ResourceException("Cannot get resource: \""
109: + resourceName + "\": " + ex);
110: } finally {
111: if (in != null) {
112: try {
113: in.close();
114: } catch (IOException ex) {
115: }
116: }
117: }
118:
119: return out;
120: }
121:
122: /** Non-instantiable class. */
123: private Resource() {
124: }
125: }
|