You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemporaryBufferEntity.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2014 Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport.http.apache;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import org.apache.http.entity.AbstractHttpEntity;
  15. import org.eclipse.jgit.util.TemporaryBuffer;
  16. /**
  17. * A {@link org.apache.http.HttpEntity} which takes its content from a
  18. * {@link org.eclipse.jgit.util.TemporaryBuffer}
  19. *
  20. * @since 3.3
  21. */
  22. public class TemporaryBufferEntity extends AbstractHttpEntity
  23. implements AutoCloseable {
  24. private TemporaryBuffer buffer;
  25. private Integer contentLength;
  26. /**
  27. * Construct a new {@link org.apache.http.HttpEntity} which will contain the
  28. * content stored in the specified buffer
  29. *
  30. * @param buffer
  31. */
  32. public TemporaryBufferEntity(TemporaryBuffer buffer) {
  33. this.buffer = buffer;
  34. }
  35. /**
  36. * Get the <code>buffer</code> containing the content
  37. *
  38. * @return buffer containing the content
  39. */
  40. public TemporaryBuffer getBuffer() {
  41. return buffer;
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public boolean isRepeatable() {
  46. return true;
  47. }
  48. /** {@inheritDoc} */
  49. @Override
  50. public long getContentLength() {
  51. if (contentLength != null)
  52. return contentLength.intValue();
  53. return buffer.length();
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public InputStream getContent() throws IOException, IllegalStateException {
  58. return buffer.openInputStream();
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public void writeTo(OutputStream outstream) throws IOException {
  63. // TODO: dont we need a progressmonitor
  64. buffer.writeTo(outstream, null);
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. public boolean isStreaming() {
  69. return false;
  70. }
  71. /**
  72. * Set the <code>contentLength</code>
  73. *
  74. * @param contentLength
  75. */
  76. public void setContentLength(int contentLength) {
  77. this.contentLength = Integer.valueOf(contentLength);
  78. }
  79. /**
  80. * {@inheritDoc}
  81. *
  82. * Close destroys the associated buffer used to buffer the entity
  83. * @since 4.5
  84. */
  85. @Override
  86. public void close() {
  87. if (buffer != null) {
  88. buffer.destroy();
  89. }
  90. }
  91. }