public InputStream getContentStream(String version) throws PortalException {

    if (Validator.isNull(version)) {
      return getContentStream();
    }

    for (Document document : getAllVersions()) {
      if (version.equals(document.getVersionLabel())) {
        ContentStream contentStream = document.getContentStream();

        try {
          DLAppHelperLocalServiceUtil.getFileAsStream(PrincipalThreadLocal.getUserId(), this, true);
        } catch (Exception e) {
          _log.error(e, e);
        }

        return contentStream.getStream();
      }
    }

    throw new NoSuchFileVersionException(
        "No CMIS file version with {fileEntryId="
            + getFileEntryId()
            + ", version="
            + version
            + "}");
  }
  public InputStream getContentStream() {
    ContentStream contentStream = _document.getContentStream();

    try {
      DLAppHelperLocalServiceUtil.getFileAsStream(PrincipalThreadLocal.getUserId(), this, true);
    } catch (Exception e) {
      _log.error(e, e);
    }

    return contentStream.getStream();
  }
  public InputStream getContentStream(boolean incrementCounter) {
    ContentStream contentStream = _document.getContentStream();

    try {
      DLAppHelperLocalServiceUtil.getFileAsStream(
          PrincipalThreadLocal.getUserId(), getFileEntry(), incrementCounter);
    } catch (Exception e) {
      _log.error(e);
    }

    return contentStream.getStream();
  }
 @Override
 public void constraintContentStreamRequired(
     DocumentTypeDefinition typeDefinition, ContentStream contentStream) {
   if (ContentStreamAllowed.REQUIRED.equals(typeDefinition.getContentStreamAllowed())) {
     if (contentStream == null || contentStream.getStream() == null) {
       constraint(
           "[typeId="
               + typeDefinition.getId()
               + "]This document type does not allow no content stream");
     }
   }
 }
  @Override
  public ContentStream setContent(StoredObject so, ContentStream contentStream) {
    if (so instanceof Content) {
      ContentStreamDataImpl newContent;
      Content content = (Content) so;

      if (null == contentStream) {
        newContent = null;
      } else {
        boolean useFakeContentStore =
            so.getTypeId().equals(DefaultTypeSystemCreator.BIG_CONTENT_FAKE_TYPE);
        newContent =
            new ContentStreamDataImpl(
                MAX_CONTENT_SIZE_KB == null ? 0 : MAX_CONTENT_SIZE_KB, useFakeContentStore);
        String fileName = contentStream.getFileName();
        if (null == fileName || fileName.length() <= 0) {
          fileName = so.getName(); // use name of document as fallback
        }
        newContent.setFileName(fileName);
        String mimeType = contentStream.getMimeType();
        if (null == mimeType || mimeType.length() <= 0) {
          mimeType = "application/octet-stream"; // use as fallback
        }
        newContent.setMimeType(mimeType);
        newContent.setLastModified(new GregorianCalendar());
        try {
          newContent.setContent(contentStream.getStream());
        } catch (IOException e) {
          throw new CmisRuntimeException("Failed to get content from InputStream", e);
        }
      }
      content.setContent(newContent);
      return newContent;

    } else {
      throw new CmisInvalidArgumentException(
          "Cannot set content, object does not implement interface Content.");
    }
  }
  @Override
  public void appendContent(StoredObject so, ContentStream contentStream) {
    if (so instanceof Content) {
      Content content = (Content) so;
      ContentStreamDataImpl newContent = (ContentStreamDataImpl) content.getContent();

      if (null == newContent) {
        content.setContent(null);
      } else {
        try {
          newContent.appendContent(contentStream.getStream());
        } catch (IOException e) {
          throw new CmisStorageException("Failed to append content: IO Exception", e);
        }
      }
    } else {
      throw new CmisInvalidArgumentException(
          "Cannot set content, object does not implement interface Content.");
    }
  }
  private void assertThatContentStreamIsSameAs(
      Document document, ContentStream stream, String hash) {
    ContentStream contentStream = document.getContentStream();
    assertThat(contentStream.getMimeType()).isEqualTo(stream.getMimeType()); // OK
    assertThat(contentStream.getFileName()).isEqualTo(stream.getFileName()); // OK
    assertThat(document.getContentStreamLength()).isEqualTo(stream.getLength());
    // ContentStream lenghts are accessible in the document, not needed in the stream itself.
    assertThat(contentStream.getBigLength()).isNull();
    assertThat(contentStream.getLength()).isEqualTo(-1);

    assertThat(contentStream.getStream()).hasContentEqualTo(stream.getStream()); // OK

    // All of those are MISSING
    assertThat(document.getContentStreamId()).isEqualTo(hash);
    assertThat(document.getContentStreamFileName()).isEqualTo(stream.getFileName());
    assertThat(document.getContentStreamLength()).isEqualTo(stream.getLength());
    assertThat(document.getContentStreamMimeType()).isEqualTo(stream.getMimeType());
  }