Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.documentlibrary.service.impl; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.io.delta.ByteChannelReader; import com.liferay.portal.kernel.io.delta.ByteChannelWriter; import com.liferay.portal.kernel.io.delta.DeltaUtil; import com.liferay.portal.kernel.repository.model.FileEntry; import com.liferay.portal.kernel.util.FileUtil; import com.liferay.portal.kernel.util.StreamUtil; import com.liferay.portal.service.ServiceContext; import com.liferay.portlet.documentlibrary.model.DLSync; import com.liferay.portlet.documentlibrary.model.DLSyncUpdate; import com.liferay.portlet.documentlibrary.service.base.DLSyncServiceBaseImpl; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.util.Date; import java.util.List; /** * @author Michael Young */ public class DLSyncServiceImpl extends DLSyncServiceBaseImpl { public DLSyncUpdate getDLSyncUpdate(long companyId, long repositoryId, Date lastAccessDate) throws SystemException { Date now = new Date(); List<DLSync> dlSyncs = null; if (lastAccessDate != null) { dlSyncs = dlSyncPersistence.findByC_M_R(companyId, lastAccessDate, repositoryId); } DLSyncUpdate dlSyncUpdate = new DLSyncUpdate(dlSyncs, now); return dlSyncUpdate; } public InputStream getFileDeltaAsStream(long fileEntryId, String sourceVersion, String destinationVersion) throws PortalException, SystemException { InputStream deltaInputStream = null; FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId); InputStream sourceInputStream = null; File sourceFile = FileUtil.createTempFile(); FileInputStream sourceFileInputStream = null; FileChannel sourceFileChannel = null; File checksumsFile = FileUtil.createTempFile(); OutputStream checksumsOutputStream = null; WritableByteChannel checksumsWritableByteChannel = null; try { sourceInputStream = fileEntry.getContentStream(sourceVersion); FileUtil.write(sourceFile, sourceInputStream); sourceFileInputStream = new FileInputStream(sourceFile); sourceFileChannel = sourceFileInputStream.getChannel(); checksumsOutputStream = new FileOutputStream(checksumsFile); checksumsWritableByteChannel = Channels.newChannel(checksumsOutputStream); ByteChannelWriter checksumsByteChannelWriter = new ByteChannelWriter(checksumsWritableByteChannel); DeltaUtil.checksums(sourceFileChannel, checksumsByteChannelWriter); checksumsByteChannelWriter.finish(); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(sourceFileInputStream); StreamUtil.cleanUp(sourceFileChannel); StreamUtil.cleanUp(checksumsOutputStream); StreamUtil.cleanUp(checksumsWritableByteChannel); FileUtil.delete(sourceFile); } InputStream destinationInputStream = null; ReadableByteChannel destinationReadableByteChannel = null; InputStream checksumsInputStream = null; ReadableByteChannel checksumsReadableByteChannel = null; OutputStream deltaOutputStream = null; WritableByteChannel deltaOutputStreamWritableByteChannel = null; try { destinationInputStream = fileEntry.getContentStream(destinationVersion); destinationReadableByteChannel = Channels.newChannel(destinationInputStream); checksumsInputStream = new FileInputStream(checksumsFile); checksumsReadableByteChannel = Channels.newChannel(checksumsInputStream); ByteChannelReader checksumsByteChannelReader = new ByteChannelReader(checksumsReadableByteChannel); File deltaFile = FileUtil.createTempFile(); deltaOutputStream = new FileOutputStream(deltaFile); deltaOutputStreamWritableByteChannel = Channels.newChannel(deltaOutputStream); ByteChannelWriter deltaByteChannelWriter = new ByteChannelWriter(deltaOutputStreamWritableByteChannel); DeltaUtil.delta(destinationReadableByteChannel, checksumsByteChannelReader, deltaByteChannelWriter); deltaByteChannelWriter.finish(); deltaInputStream = new FileInputStream(deltaFile); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(destinationInputStream); StreamUtil.cleanUp(destinationReadableByteChannel); StreamUtil.cleanUp(checksumsInputStream); StreamUtil.cleanUp(checksumsReadableByteChannel); StreamUtil.cleanUp(deltaOutputStream); StreamUtil.cleanUp(deltaOutputStreamWritableByteChannel); FileUtil.delete(checksumsFile); } return deltaInputStream; } public FileEntry updateFileEntry(long fileEntryId, String sourceFileName, String mimeType, String title, String description, String changeLog, boolean majorVersion, InputStream deltaInputStream, long size, ServiceContext serviceContext) throws PortalException, SystemException { FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId); InputStream originalInputStream = null; File patchedFile = null; InputStream patchedInputStream = null; try { originalInputStream = fileEntry.getContentStream(); patchedFile = FileUtil.createTempFile(); patchFile(originalInputStream, deltaInputStream, patchedFile); patchedInputStream = new FileInputStream(patchedFile); return dlAppService.updateFileEntry(fileEntryId, sourceFileName, mimeType, title, description, changeLog, majorVersion, patchedInputStream, size, serviceContext); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(originalInputStream); StreamUtil.cleanUp(patchedInputStream); FileUtil.delete(patchedFile); } } protected void patchFile(InputStream originalInputStream, InputStream deltaInputStream, File patchedFile) throws PortalException { File originalFile = null; FileInputStream originalFileInputStream = null; FileChannel originalFileChannel = null; FileOutputStream patchedFileOutputStream = null; WritableByteChannel patchedWritableByteChannel = null; ReadableByteChannel deltaReadableByteChannel = null; try { originalFile = FileUtil.createTempFile(); FileUtil.write(originalFile, originalInputStream); originalFileInputStream = new FileInputStream(originalFile); originalFileChannel = originalFileInputStream.getChannel(); patchedFileOutputStream = new FileOutputStream(patchedFile); patchedWritableByteChannel = Channels.newChannel(patchedFileOutputStream); deltaReadableByteChannel = Channels.newChannel(deltaInputStream); ByteChannelReader deltaByteChannelReader = new ByteChannelReader(deltaReadableByteChannel); DeltaUtil.patch(originalFileChannel, patchedWritableByteChannel, deltaByteChannelReader); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(originalFileInputStream); StreamUtil.cleanUp(originalFileChannel); StreamUtil.cleanUp(patchedFileOutputStream); StreamUtil.cleanUp(patchedWritableByteChannel); StreamUtil.cleanUp(deltaReadableByteChannel); FileUtil.delete(originalFile); } } }