/api/src/main/java/org/jboss/shrinkwrap/api/asset/StringAsset.java

https://github.com/maschmid/shrinkwrap · Java · 83 lines · 32 code · 12 blank · 39 comment · 3 complexity · 3e50bf50dcaa274e73fa1666fd1f3cf2 MD5 · raw file

  1. package org.jboss.shrinkwrap.api.asset;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.InputStream;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. /**
  7. * Implementation of an {@link Asset} backed by a String
  8. *
  9. * @author <a href="mailto:dan.j.allen@gmail.com">Dan Allen</a>
  10. * @version $Revision: $
  11. */
  12. public class StringAsset implements Asset
  13. {
  14. //-------------------------------------------------------------------------------------||
  15. // Class Members ----------------------------------------------------------------------||
  16. //-------------------------------------------------------------------------------------||
  17. /**
  18. * Logger
  19. */
  20. private static final Logger log = Logger.getLogger(StringAsset.class.getName());
  21. //-------------------------------------------------------------------------------------||
  22. // Instance Members -------------------------------------------------------------------||
  23. //-------------------------------------------------------------------------------------||
  24. /**
  25. * Underlying content.
  26. */
  27. private final String content;
  28. //-------------------------------------------------------------------------------------||
  29. // Constructor ------------------------------------------------------------------------||
  30. //-------------------------------------------------------------------------------------||
  31. /**
  32. * Creates a new {@link Asset} instance backed by the specified String
  33. *
  34. * @param content The content represented as a String
  35. * @throws IllegalArgumentException If the contents were not specified
  36. */
  37. public StringAsset(final String content)
  38. {
  39. // Precondition check
  40. if (content == null)
  41. {
  42. throw new IllegalArgumentException("content must be specified");
  43. }
  44. // don't need to copy since String is immutable
  45. this.content = content;
  46. if (log.isLoggable(Level.FINER))
  47. {
  48. log.finer("Created " + this + " with backing String of size " + content.length() + "b");
  49. }
  50. }
  51. //-------------------------------------------------------------------------------------||
  52. // Required Implementations -----------------------------------------------------------||
  53. //-------------------------------------------------------------------------------------||
  54. /**
  55. * @see org.jboss.shrinkwrap.api.asset.Asset#openStream()
  56. */
  57. @Override
  58. public InputStream openStream()
  59. {
  60. return new ByteArrayInputStream(content.getBytes());
  61. }
  62. /**
  63. * {@inheritDoc}
  64. * @see java.lang.Object#toString()
  65. */
  66. @Override
  67. public String toString()
  68. {
  69. return StringAsset.class.getSimpleName() + " [content size=" + content.length() + " bytes]";
  70. }
  71. }