Merged CMIS063 to HEAD

16930: MOB-1330: Upgrade Web Services Repository to 0.7

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@17243 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2009-10-29 18:40:59 +00:00
parent 93346e262e
commit d6b8ce91a5
203 changed files with 10223 additions and 6060 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2008 Alfresco Software Limited.
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -27,6 +27,7 @@ package org.alfresco.repo.cmis.ws;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import javax.activation.DataSource;
@@ -39,53 +40,141 @@ import org.alfresco.service.cmr.repository.ContentReader;
*/
public class ContentReaderDataSource implements DataSource
{
private ContentReader contentReader;
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)
{
this.contentReader = contentReader;
public ContentReaderDataSource(ContentReader contentReader, String name, BigInteger offset, BigInteger length, long contentSize)
{
this.name = name;
this.mimetype = contentReader.getMimetype();
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(contentReader.getContentInputStream());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/*
* (non-Javadoc)
*
* @see javax.activation.DataSource#getContentType()
*/
public String getContentType()
{
return contentReader.getMimetype();
return mimetype;
}
/*
* (non-Javadoc)
*
* @see javax.activation.DataSource#getInputStream()
*/
public InputStream getInputStream() throws IOException
{
return contentReader.getContentInputStream();
return inputStream;
}
/*
* (non-Javadoc)
*
* @see javax.activation.DataSource#getName()
*/
public String getName()
{
return name;
}
/*
* (non-Javadoc)
*
* @see javax.activation.DataSource#getOutputStream()
*/
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;
}
}
}