
com.gc.iotools.stream.store.OnOffStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easystream Show documentation
Show all versions of easystream Show documentation
EasyStream is a small set of utilities for dealing with
streams (InputStreams
and OutputStreams).
The aim is to ease the use of
pipes when they're required.
Main features are:
* "Convert" an
OutputStream to an InputStream.
* Count the number of bytes read or
wrote to a given stream.
* While reading the data from an InputStream
copy it to a supplied
OutputStream.
* Read the content of an InputStream
multiple times or seek to a
definite position
The newest version!
package com.gc.iotools.stream.store;
/*
* Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
* under the BSD License.
*/
import java.io.IOException;
/**
* OnOffStore class.
*
* @author gcontini
* @version $Id: OnOffStore.java 576 2015-03-28 00:03:33Z gcontini $
*/
public class OnOffStore implements SeekableStore {
private boolean canEnable = true;
private boolean enabled = true;
private final SeekableStore store;
/**
* Constructor for OnOffStore.
*
* @param store a {@link com.gc.iotools.stream.store.SeekableStore} object.
*/
public OnOffStore(final SeekableStore store) {
this.store = store;
}
/** {@inheritDoc} */
@Override
public void cleanup() {
this.store.cleanup();
}
/**
* enable
*
* @param enable a boolean.
*/
public void enable(final boolean enable) {
if (enable != this.enabled) {
if (enable) {
if (!this.canEnable) {
throw new IllegalStateException(
"Enable was called but some "
+ "data was already put on the buffer. "
+ "Can't reenable.");
}
}
this.enabled = enable;
}
}
/** {@inheritDoc} */
@Override
public int get(final byte[] bytes, final int offset, final int length)
throws IOException {
final int num = this.store.get(bytes, offset, length);
if (!this.enabled && num == 0) {
cleanup();
}
return num;
}
/** {@inheritDoc} */
@Override
public void put(final byte[] bytes, final int offset, final int length)
throws IOException {
if (this.enabled) {
this.store.put(bytes, offset, length);
} else {
this.canEnable = false;
}
}
/** {@inheritDoc} */
@Override
public void seek(final long position) throws IOException {
this.store.seek(position);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy