1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ByteArrayRequestEntity.java,v 1.3 2004/05/13 02:26:08 mbecke Exp $
  3. * $Revision: 1.3 $
  4. * $Date: 2004/05/13 02:26:08 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. * [Additional notices, if required by prior licensing conditions]
  29. *
  30. */
  31. package org.apache.commons.httpclient.methods;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. /**
  35. * A RequestEntity that contains an array of bytes.
  36. *
  37. * @since 3.0
  38. */
  39. public class ByteArrayRequestEntity implements RequestEntity {
  40. /** The content */
  41. private byte[] content;
  42. /** The content type */
  43. private String contentType;
  44. /**
  45. * Creates a new entity with the given content.
  46. * @param content The content to set.
  47. */
  48. public ByteArrayRequestEntity(byte[] content) {
  49. this(content, null);
  50. }
  51. /**
  52. * Creates a new entity with the given content and content type.
  53. * @param content The content to set.
  54. * @param contentType The content type to set or <code>null</code>.
  55. */
  56. public ByteArrayRequestEntity(byte[] content, String contentType) {
  57. super();
  58. if (content == null) {
  59. throw new IllegalArgumentException("The content cannot be null");
  60. }
  61. this.content = content;
  62. this.contentType = contentType;
  63. }
  64. /**
  65. * @return <code>true</code>
  66. */
  67. public boolean isRepeatable() {
  68. return true;
  69. }
  70. /* (non-Javadoc)
  71. * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
  72. */
  73. public String getContentType() {
  74. return contentType;
  75. }
  76. /* (non-Javadoc)
  77. * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
  78. */
  79. public void writeRequest(OutputStream out) throws IOException {
  80. out.write(content);
  81. }
  82. /**
  83. * @return The length of the content.
  84. */
  85. public long getContentLength() {
  86. return content.length;
  87. }
  88. /**
  89. * @return Returns the content.
  90. */
  91. public byte[] getContent() {
  92. return content;
  93. }
  94. }