Skip to content

Commit

Permalink
#87 initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Jun 2, 2015
1 parent 559bddc commit 0924ace
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package org.irods.jargon.core.pub.io;

import java.io.FileNotFoundException;

/**
* Wrap an iRODS input stream in an accumulating buffer that will emulate reads
* from a continuous stream while fetching chunks from iRODS in a more optimal
* size
*
* @author Mike Conway - DICE
*
*/
public class PackingIrodsInputStream extends IRODSFileInputStream {

/**
* @param irodsFile
* @param fileIOOperations
* @throws FileNotFoundException
*/
public PackingIrodsInputStream(IRODSFile irodsFile,
FileIOOperations fileIOOperations) throws FileNotFoundException {
super(irodsFile, fileIOOperations);
}

/**
* @param irodsFile
* @param fileIOOperations
* @param fd
* @throws FileNotFoundException
*/
public PackingIrodsInputStream(IRODSFile irodsFile,
FileIOOperations fileIOOperations, int fd)
throws FileNotFoundException {
super(irodsFile, fileIOOperations, fd);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
*
*/
package org.irods.jargon.core.pub.io;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import org.irods.jargon.core.exception.JargonException;
import org.irods.jargon.core.exception.NoResourceDefinedException;

/**
* A stream that presents the normal api, but accumulates bytes written until at
* an optimal buffer size before sending to iRODS
*
* @author Mike Conway - DICE
*
*/
public class PackingIrodsOutputStream extends OutputStream {

public final int BUFFER_SIZE = 4 * 1024 * 1024; // FIXME: make this a jargon
// props later
private int byteBufferSizeMax = 32 * 1024;
private int ptr = 0;
private ByteArrayOutputStream byteArrayOutputStream = null;
private final IRODSFileOutputStream irodsFileOutputStream;

/**
* @param irodsFile
* @param fileIOOperations
* @param openFlags
* @throws NoResourceDefinedException
* @throws FileNotFoundException
* @throws JargonException
*/
public PackingIrodsOutputStream(IRODSFileOutputStream irodsFileOutputStream)
throws NoResourceDefinedException, FileNotFoundException,
JargonException {
if (irodsFileOutputStream == null) {
throw new IllegalArgumentException("null irodsFileOutputStream");
}
if (byteBufferSizeMax <= 0) {
throw new IllegalStateException(
"cannot have a zero or negative buffer size");
}
byteArrayOutputStream = new ByteArrayOutputStream();
this.irodsFileOutputStream = irodsFileOutputStream;
}

/*
* (non-Javadoc)
*
* @see org.irods.jargon.core.pub.io.IRODSFileOutputStream#write(byte[],
* int, int)
*/
@Override
public void write(byte[] b, int off, int len) throws IOException {
int projectedLen = this.ptr + len;

if (projectedLen > byteBufferSizeMax) {
// would overflow the buff, so write partial to iRODS and start a
// new buffer
int lenToHoldOver = projectedLen - byteBufferSizeMax;
int lenToAddToBuff = len - lenToHoldOver;
byteArrayOutputStream.write(b, off, lenToAddToBuff);
irodsFileOutputStream
}

super.write(b, off, len);
}

/*
* (non-Javadoc)
*
* @see org.irods.jargon.core.pub.io.IRODSFileOutputStream#write(byte[])
*/
@Override
public void write(byte[] b) throws IOException {
// TODO Auto-generated method stub
super.write(b);
}

/*
* (non-Javadoc)
*
* @see org.irods.jargon.core.pub.io.IRODSFileOutputStream#write(int)
*/
@Override
public void write(int b) throws IOException {
// TODO Auto-generated method stub
super.write(b);
}

/**
* @return the byteBufferSize
*/
public int getByteBufferSize() {
return byteBufferSize;
}

/**
* @param byteBufferSize
* the byteBufferSize to set
*/
public void setByteBufferSize(int byteBufferSize) {
this.byteBufferSize = byteBufferSize;
}

}

0 comments on commit 0924ace

Please sign in to comment.