Example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayInputStream available

List of usage examples for com.liferay.portal.kernel.io.unsync UnsyncByteArrayInputStream available

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.io.unsync UnsyncByteArrayInputStream available.

Prototype

@Override
    public int available() 

Source Link

Usage

From source file:com.liferay.portlet.documentlibrary.store.DBStore.java

License:Open Source License

@Override
public void updateFile(long companyId, long repositoryId, String fileName, String versionLabel,
        InputStream inputStream) throws PortalException, SystemException {

    if (DLContentLocalServiceUtil.hasContent(companyId, repositoryId, fileName, versionLabel)) {

        throw new DuplicateFileException(fileName);
    }// w w  w  .j ava2s .co  m

    long length = -1;

    if (inputStream instanceof ByteArrayInputStream) {
        ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) inputStream;

        length = byteArrayInputStream.available();
    } else if (inputStream instanceof FileInputStream) {
        FileInputStream fileInputStream = (FileInputStream) inputStream;

        FileChannel fileChannel = fileInputStream.getChannel();

        try {
            length = fileChannel.size();
        } catch (IOException ioe) {
            if (_log.isWarnEnabled()) {
                _log.warn("Unable to detect file size from file channel", ioe);
            }
        }
    } else if (inputStream instanceof UnsyncByteArrayInputStream) {
        UnsyncByteArrayInputStream unsyncByteArrayInputStream = (UnsyncByteArrayInputStream) inputStream;

        length = unsyncByteArrayInputStream.available();
    }

    if (length >= 0) {
        DLContentLocalServiceUtil.addContent(companyId, repositoryId, fileName, versionLabel, inputStream,
                length);
    } else {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to detect length from input stream. Reading "
                    + "entire input stream into memory as a last resort.");
        }

        byte[] bytes = null;

        try {
            bytes = FileUtil.getBytes(inputStream);
        } catch (IOException ioe) {
            throw new SystemException(ioe);
        }

        DLContentLocalServiceUtil.addContent(companyId, repositoryId, fileName, versionLabel, bytes);
    }
}