Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » core » io » [javadoc | source]
    1   /*
    2    * Copyright 2002-2007 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package org.springframework.core.io;
   18   
   19   import java.io.ByteArrayInputStream;
   20   import java.io.IOException;
   21   import java.io.InputStream;
   22   import java.util.Arrays;
   23   
   24   /**
   25    * {@link Resource} implementation for a given byte array.
   26    * Creates a ByteArrayInputStreams for the given byte array.
   27    *
   28    * <p>Useful for loading content from any given byte array,
   29    * without having to resort to a single-use {@link InputStreamResource}.
   30    * Particularly useful for creating mail attachments from local content,
   31    * where JavaMail needs to be able to read the stream multiple times.
   32    *
   33    * @author Juergen Hoeller
   34    * @since 1.2.3
   35    * @see java.io.ByteArrayInputStream
   36    * @see InputStreamResource
   37    * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
   38    */
   39   public class ByteArrayResource extends AbstractResource {
   40   
   41   	private final byte[] byteArray;
   42   
   43   	private final String description;
   44   
   45   
   46   	/**
   47   	 * Create a new ByteArrayResource.
   48   	 * @param byteArray the byte array to wrap
   49   	 */
   50   	public ByteArrayResource(byte[] byteArray) {
   51   		this(byteArray, "resource loaded from byte array");
   52   	}
   53   
   54   	/**
   55   	 * Create a new ByteArrayResource.
   56   	 * @param byteArray the byte array to wrap
   57   	 * @param description where the byte array comes from
   58   	 */
   59   	public ByteArrayResource(byte[] byteArray, String description) {
   60   		if (byteArray == null) {
   61   			throw new IllegalArgumentException("Byte array must not be null");
   62   		}
   63   		this.byteArray = byteArray;
   64   		this.description = (description != null ? description : "");
   65   	}
   66   
   67   	/**
   68   	 * Return the underlying byte array.
   69   	 */
   70   	public final byte[] getByteArray() {
   71   		return this.byteArray;
   72   	}
   73   
   74   
   75   	/**
   76   	 * This implementation always returns <code>true</code>.
   77   	 */
   78   	public boolean exists() {
   79   		return true;
   80   	}
   81   
   82   	/**
   83   	 * This implementation returns a ByteArrayInputStream for the
   84   	 * underlying byte array.
   85   	 * @see java.io.ByteArrayInputStream
   86   	 */
   87   	public InputStream getInputStream() throws IOException {
   88   		return new ByteArrayInputStream(this.byteArray);
   89   	}
   90   
   91   	/**
   92   	 * This implementation returns the passed-in description, if any.
   93   	 */
   94   	public String getDescription() {
   95   		return this.description;
   96   	}
   97   
   98   
   99   	/**
  100   	 * This implementation compares the underlying byte array.
  101   	 * @see java.util.Arrays#equals(byte[], byte[])
  102   	 */
  103   	public boolean equals(Object obj) {
  104   		return (obj == this ||
  105   		    (obj instanceof ByteArrayResource && Arrays.equals(((ByteArrayResource) obj).byteArray, this.byteArray)));
  106   	}
  107   
  108   	/**
  109   	 * This implementation returns the hash code based on the
  110   	 * underlying byte array.
  111   	 */
  112   	public int hashCode() {
  113   		return (byte[].class.hashCode() * 29 * this.byteArray.length);
  114   	}
  115   
  116   }

Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » core » io » [javadoc | source]