
com.alachisoft.ncache.client.internal.caching.InputCacheStream Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.client.internal.caching;
import com.alachisoft.ncache.client.CacheStream;
import com.alachisoft.ncache.client.StreamMode;
import com.alachisoft.ncache.runtime.exceptions.NotSupportedException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* InputCacheStream is derived from System.IO.InputStream. It is designed to put/fetch BLOB using standard InputStream Interface
*
* @author Administrator
*/
class InputCacheStream extends InputStream implements CacheStream {
private CacheImpl _cacheHandle;
private StreamMode _mode;
private String _key;
private long _position;
private String _lockHandle;
private boolean _closed;
private long _length;
InputCacheStream(String key, StreamMode mode, CacheImpl cacheHandle, String lockHandle, long length) {
_cacheHandle = cacheHandle;
_mode = mode;
_key = key;
_length = length;
_lockHandle = lockHandle;
_position = 0;
}
@Override
public int read() throws IOException {
//throw new UnsupportedOperationException("Not supported yet.");
byte[] buffer = new byte[1];
read(buffer, 0, 1);
return buffer[0];
}
@Override
public int read(byte b[], int off, int len) throws IOException {
if (_mode == StreamMode.Write)
throw new IOException("Stream does not support reading.");
if (_closed) throw new IOException("Methods were called after the stream was closed.");
if (b == null) throw new NullPointerException("buffer");
if (off + len > b.length) throw new IOException("Sum of offset and count is greater than the buffer length.");
if (off < 0 || len < 0) throw new IOException("offset or count is negative.");
int bytesRead;
try {
bytesRead = _cacheHandle.readFromStream(b, _key, _lockHandle, off, (int) _position, len);
} catch (Exception exception) {
throw new IOException(exception.getMessage());
}
_position += bytesRead;
return bytesRead;
}
@Override
public void write(byte[] buffer, int offset, int count) throws Exception {
}
@Override
public void close() throws IOException {
_closed = true;
//Lets release the lock on the stream.
if (_mode == StreamMode.Read) {
try {
_cacheHandle.closeStream(_key, _lockHandle);
} catch (Exception exception) {
throw new IOException(exception.getMessage());
}
}
}
@Override
public long skip(long n) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean canRead() {
return _mode != StreamMode.Write ? true : false;
}
@Override
public boolean canSeek() {
return false;
}
@Override
public boolean canWrite() {
return _mode == StreamMode.Write ? true : false;
}
@Override
public boolean closed() {
return _closed;
}
@Override
public void flush() {
}
@Override
public long length() throws Exception {
if (_closed) throw new IllegalAccessException("Methods were called after the stream was closed.");
return _cacheHandle.getStreamLength(_key, _lockHandle);
}
@Override
public long position() throws Exception {
if (!canSeek())
throw new NotSupportedException("Operation not supported");
return 0;
}
@Override
public long seek(long offset) throws NotSupportedException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void setLength(long value) throws NotSupportedException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public BufferedInputStream getBufferedInputStream(int bufferSize) throws Exception {
return null;
}
@Override
public BufferedInputStream getBufferedInputStream() throws Exception {
return null;
}
@Override
public BufferedOutputStream getBufferedOutputStream(int bufferSize) throws Exception {
return null;
}
@Override
public BufferedOutputStream getBufferedOutputStream() throws Exception {
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy