mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (5.0/Cloud)
76302: Merged EOL2 (5.0.0.EOL2) to HEAD-BUG-FIX (5.0/Cloud) 75774: ACE-2010 - EOL Legacy CMIS - removed webscript REST APIs pertinent to Alfresco 3.4 CMIS API pt2 git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@77576 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
190
source/java/org/alfresco/repo/ws/ContentReaderDataSource.java
Normal file
190
source/java/org/alfresco/repo/ws/ContentReaderDataSource.java
Normal file
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2010 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.repo.ws;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.activation.DataSource;
|
||||
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
|
||||
/**
|
||||
* DataSource facade for an Alfresco Content Reader
|
||||
*
|
||||
* @author Dmitry Lazurkin
|
||||
*/
|
||||
public class ContentReaderDataSource implements DataSource
|
||||
{
|
||||
private String mimetype;
|
||||
private InputStream inputStream;
|
||||
private String name;
|
||||
private long offset = 0;
|
||||
private long length = Long.MAX_VALUE / 2;
|
||||
private long sizeToRead = 0;
|
||||
|
||||
public ContentReaderDataSource(ContentReader contentReader, String name, BigInteger offset, BigInteger length, long contentSize)
|
||||
{
|
||||
createContentReaderDataSource(contentReader.getContentInputStream(), contentReader.getMimetype(), name, offset, length, contentSize);
|
||||
}
|
||||
|
||||
public ContentReaderDataSource(InputStream contentInputStream, String mimeType, String name, BigInteger offset, BigInteger length)
|
||||
{
|
||||
try
|
||||
{
|
||||
createContentReaderDataSource(contentInputStream, mimeType, name, offset, length, contentInputStream.available());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void createContentReaderDataSource(InputStream contentInputStream, String mimeType, String name, BigInteger offset, BigInteger length, long contentSize)
|
||||
{
|
||||
this.name = name;
|
||||
this.mimetype = mimeType;
|
||||
if (offset != null)
|
||||
{
|
||||
this.offset = offset.longValue();
|
||||
}
|
||||
if (length != null)
|
||||
{
|
||||
this.length = length.longValue();
|
||||
}
|
||||
if (this.offset + this.length < contentSize)
|
||||
{
|
||||
this.sizeToRead = this.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sizeToRead = contentSize - this.offset;
|
||||
}
|
||||
if (this.sizeToRead < 0)
|
||||
{
|
||||
throw new RuntimeException("Offset value exceeds content size");
|
||||
}
|
||||
try
|
||||
{
|
||||
inputStream = new RangedInputStream(contentInputStream);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
return mimetype;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getSizeToRead()
|
||||
{
|
||||
return sizeToRead;
|
||||
}
|
||||
|
||||
private class RangedInputStream extends InputStream
|
||||
{
|
||||
|
||||
private InputStream inputStream;
|
||||
private int bytesread;
|
||||
|
||||
private RangedInputStream(InputStream inputStream) throws IOException
|
||||
{
|
||||
super();
|
||||
this.inputStream = inputStream;
|
||||
this.inputStream.skip(offset);
|
||||
this.bytesread = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException
|
||||
{
|
||||
if (bytesread < sizeToRead)
|
||||
{
|
||||
bytesread++;
|
||||
return inputStream.read();
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException
|
||||
{
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException
|
||||
{
|
||||
if (len > sizeToRead - bytesread)
|
||||
{
|
||||
len = (int) (sizeToRead - bytesread);
|
||||
}
|
||||
int readed = inputStream.read(b, off, len);
|
||||
bytesread += readed;
|
||||
return readed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException
|
||||
{
|
||||
return (int) (sizeToRead - bytesread + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
inputStream.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException
|
||||
{
|
||||
if (bytesread + n > sizeToRead)
|
||||
{
|
||||
n = (sizeToRead - n) > 0 ? (sizeToRead - n) : sizeToRead - bytesread;
|
||||
}
|
||||
n = inputStream.skip(n);
|
||||
bytesread += n;
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user