[jboss-svn-commits] JBoss Common SVN: r3486 - in declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base: asset and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Aug 26 03:38:45 EDT 2009
Author: ALRubinger
Date: 2009-08-26 03:38:44 -0400 (Wed, 26 Aug 2009)
New Revision: 3486
Added:
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java
Modified:
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/asset/ByteArrayAsset.java
Log:
[TMPARCH-21] Make a ByteArrayAsset from an InputStream
Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/asset/ByteArrayAsset.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/asset/ByteArrayAsset.java 2009-08-26 07:16:38 UTC (rev 3485)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/asset/ByteArrayAsset.java 2009-08-26 07:38:44 UTC (rev 3486)
@@ -23,6 +23,7 @@
import org.jboss.declarchive.api.Asset;
import org.jboss.declarchive.impl.base.Validate;
+import org.jboss.declarchive.impl.base.io.IOUtil;
/**
* ByteArrayAsset
@@ -58,7 +59,7 @@
//-------------------------------------------------------------------------------------||
/**
- * Creates a new instance backed by the specified
+ * Creates a new {@link Asset} instance backed by the specified
* byte array
*
* @param content
@@ -77,6 +78,19 @@
}
}
+ /**
+ * Creates a new {@link Asset} instance backed by the bytes
+ * contained in the the specified {@link InputStream}
+ *
+ * @param stream
+ * @throws IllegalArgumentException If the stream is not specified
+ */
+ public ByteArrayAsset(final InputStream stream)
+ {
+ // Delegate
+ this(IOUtil.asByteArray(stream));
+ }
+
//-------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -89,4 +103,14 @@
{
return new ByteArrayInputStream(this.content);
}
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return "ByteArrayAsset [content size=" + content.length + "bytes]";
+ }
}
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java 2009-08-26 07:38:44 UTC (rev 3486)
@@ -0,0 +1,115 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.declarchive.impl.base.io;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.jboss.declarchive.impl.base.Validate;
+
+/**
+ * IOUtil
+ *
+ * Generic input/output utilities
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class IOUtil
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Internal constructor; should not be called
+ */
+ private IOUtil()
+ {
+ throw new UnsupportedOperationException("No instances should be created; stateless class");
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the contents of the specified stream
+ * as a byte array
+ *
+ * @param in
+ * @throws IllegalArgumentException If the stream was not specified
+ */
+ public static byte[] asByteArray(final InputStream in) throws IllegalArgumentException
+ {
+ // Precondition check
+ Validate.notNull(in, "stream must be specified");
+
+ // Get content as an array of bytes
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final int len = 1024;
+ final byte[] buffer = new byte[len];
+ try
+ {
+
+ while ((in.read(buffer) != -1))
+ {
+ out.write(buffer);
+ }
+ }
+ catch (final IOException ioe)
+ {
+ throw new RuntimeException("Error in obtainting bytes from " + in, ioe);
+ }
+ finally
+ {
+ try
+ {
+ in.close();
+ }
+ catch (final IOException ignore)
+ {
+
+ }
+ // We don't need to close the outstream, it's a byte array out
+ }
+
+ // Represent as byte array
+ final byte[] content = out.toByteArray();
+
+ // Return
+ return content;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+}
More information about the jboss-svn-commits
mailing list