[jboss-svn-commits] JBoss Common SVN: r4345 - in shrinkwrap/trunk/impl-base/src: main/java/org/jboss/shrinkwrap/impl/base/io and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon May 3 14:38:45 EDT 2010


Author: ALRubinger
Date: 2010-05-03 14:38:44 -0400 (Mon, 03 May 2010)
New Revision: 4345

Added:
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java
Modified:
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java
Log:
[SHRINKWRAP-165] Applied Dan Allen patch to create StringAsset, just removed ctor to construct from InputStream

Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/StringAsset.java	2010-05-03 18:38:44 UTC (rev 4345)
@@ -0,0 +1,83 @@
+package org.jboss.shrinkwrap.impl.base.asset;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.impl.base.Validate;
+
+/**
+ * Implementation of a {@link Asset} backed by a String
+ *
+ * @author <a href="mailto:dan.j.allen at gmail.com">Dan Allen</a>
+ * @version $Revision: $
+ */
+public class StringAsset implements Asset
+{
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(StringAsset.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Underlying content.
+    */
+   private final String content;
+
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates a new {@link Asset} instance backed by the specified String
+    * 
+    * @param content The content represented as a String
+    * @throws IllegalArgumentException If the contents were not specified
+    */
+   public StringAsset(final String content)
+   {
+      // Precondition check
+      Validate.notNull(content, "content must be specified");
+      // don't need to copy since String is immutable
+      this.content = content;
+      if (log.isLoggable(Level.FINER))
+      {
+         log.finer("Created " + this + " with backing String of size " + content.length() + "b");
+      }
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * @see org.jboss.shrinkwrap.api.Asset#openStream()
+    */
+
+   @Override
+   public InputStream openStream()
+   {
+      return new ByteArrayInputStream(content.getBytes());
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see java.lang.Object#toString()
+    */
+   @Override
+   public String toString()
+   {
+      return "StringAsset [content size=" + content.length() + " bytes]";
+   }
+
+}

Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java	2010-05-03 18:16:36 UTC (rev 4344)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java	2010-05-03 18:38:44 UTC (rev 4345)
@@ -16,10 +16,12 @@
  */
 package org.jboss.shrinkwrap.impl.base.io;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -57,6 +59,11 @@
       }
 
    };
+   
+   /**
+    * Name of UTF-8 Charset
+    */
+   private static final String CHARSET_UTF8 = "UTF-8";
 
    //-------------------------------------------------------------------------------------||
    // Constructor ------------------------------------------------------------------------||
@@ -126,6 +133,51 @@
    }
 
    /**
+    * Obtains the contents of the specified stream
+    * as a String in UTF-8 charset.
+    * 
+    * @param in
+    * @throws IllegalArgumentException If the stream was not specified
+    */
+   public static String asUTF8String(InputStream in)
+   {
+      // Precondition check
+      Validate.notNull(in, "Stream must be specified");
+
+      StringBuilder buffer = new StringBuilder();
+      String line;
+
+      try
+      {
+         BufferedReader reader = new BufferedReader(new InputStreamReader(in, CHARSET_UTF8));
+         while ((line = reader.readLine()) != null)
+         {
+            buffer.append(line).append(Character.LINE_SEPARATOR);
+         }
+      }
+      catch (IOException ioe)
+      {
+         throw new RuntimeException("Error in obtaining string from " + in, ioe);
+      }
+      finally
+      {
+         try
+         {
+            in.close();
+         }
+         catch (IOException ignore)
+         {
+            if (log.isLoggable(Level.FINER))
+            {
+               log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring");
+            }
+         }
+      }
+
+      return buffer.toString();
+   }
+
+   /**
     * Copies the contents from an InputStream to an OutputStream.  It is the
     * responsibility of the caller to close the streams passed in when done, 
     * though the {@link OutputStream} will be fully flushed.

Added: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/asset/StringAssetTestCase.java	2010-05-03 18:38:44 UTC (rev 4345)
@@ -0,0 +1,78 @@
+/*
+ * 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.shrinkwrap.impl.base.asset;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.logging.Logger;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+/**
+ * Test Cases for the {@link StringAsset}
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @author <a href="mailto:dan.j.allen at gmail.com">Dan Allen</a>
+ * @version $Revision: $
+ */
+public class StringAssetTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(StringAssetTestCase.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that the contents of the asset match that which was passed in.
+    */
+   @Test
+   public void testRoundtrip() throws Exception
+   {
+      // Log
+      log.info("testRoundtrip");
+
+      // Make contents
+      String contents = StringAsset.class.getSimpleName();
+
+      // Make Asset
+      final StringAsset asset = new StringAsset(contents);
+
+      // Get the contents back out of the asset
+      final InputStream stream = asset.openStream();
+      final ByteArrayOutputStream out = new ByteArrayOutputStream(contents.length());
+      int read;
+      while ((read = stream.read()) != -1)
+      {
+         out.write(read);
+      }
+      String roundtrip = new String(out.toByteArray());
+      log.info("Roundtrip contents: " + roundtrip);
+
+      Assert.assertEquals("Roundtrip did not equal passed in contents", contents, roundtrip);
+   }
+}



More information about the jboss-svn-commits mailing list