Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#378: ReaderOf, InputStreamOf, OutputStreamOf, etc. #379

Merged
merged 6 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/org/cactoos/io/BytesOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
package org.cactoos.io;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
Expand Down Expand Up @@ -59,6 +61,24 @@ public BytesOf(final Input input) {
this(new InputAsBytes(input));
}

/**
* Ctor.
* @param file The input
* @since 0.13
*/
public BytesOf(final File file) {
this(new InputOf(file));
}

/**
* Ctor.
* @param path The input
* @since 0.13
*/
public BytesOf(final Path path) {
this(new InputOf(path));
}

/**
* Ctor.
*
Expand Down
158 changes: 158 additions & 0 deletions src/main/java/org/cactoos/io/InputStreamOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Wrapper of {@link InputStream}.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.13
*/
public final class InputStreamOf extends InputStream {

/**
* The source.
*/
private final UncheckedScalar<InputStream> source;

/**
* Ctor.
* @param path The path
*/
public InputStreamOf(final Path path) {
this(new InputOf(path));
}

/**
* Ctor.
* @param file The file
*/
public InputStreamOf(final File file) {
this(new InputOf(file));
}

/**
* Ctor.
* @param url The URL
*/
public InputStreamOf(final URL url) {
this(new InputOf(url));
}

/**
* Ctor.
* @param uri The URI
*/
public InputStreamOf(final URI uri) {
this(new InputOf(uri));
}

/**
* Ctor.
* @param bytes The text
*/
public InputStreamOf(final Bytes bytes) {
this(new InputOf(bytes));
}

/**
* Ctor.
* @param text The text
*/
public InputStreamOf(final Text text) {
this(new InputOf(text));
}

/**
* Ctor.
* @param text The text
*/
public InputStreamOf(final String text) {
this(new InputOf(text));
}

/**
* Ctor.
* @param rdr The reader
*/
private InputStreamOf(final Reader rdr) {
this(new InputOf(rdr));
}

/**
* Ctor.
* @param input The input
*/
public InputStreamOf(final Input input) {
this((Scalar<InputStream>) input::stream);
}

/**
* Ctor.
* @param src Source
*/
private InputStreamOf(final Scalar<InputStream> src) {
super();
this.source = new UncheckedScalar<>(new StickyScalar<>(src));
}

@Override
public int read() throws IOException {
return this.source.value().read();
}

@Override
public int read(final byte[] buffer) throws IOException {
return this.source.value().read(buffer);
}

@Override
public int read(final byte[] buffer, final int offset,
final int length) throws IOException {
return this.source.value().read(buffer, offset, length);
}

@Override
public void close() throws IOException {
this.source.value().close();
}

}
114 changes: 114 additions & 0 deletions src/main/java/org/cactoos/io/OutputStreamTo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.io;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.file.Path;
import org.cactoos.Output;
import org.cactoos.Scalar;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Wrapper of {@link OutputStream}.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.13
*/
public final class OutputStreamTo extends OutputStream {

/**
* The target.
*/
private final UncheckedScalar<OutputStream> target;

/**
* Ctor.
* @param path The path
*/
public OutputStreamTo(final Path path) {
this(new OutputTo(path));
}

/**
* Ctor.
* @param file The file
*/
public OutputStreamTo(final File file) {
this(new OutputTo(file));
}

/**
* Ctor.
* @param wtr The writer
*/
private OutputStreamTo(final Writer wtr) {
this(new OutputTo(wtr));
}

/**
* Ctor.
* @param output The input
*/
public OutputStreamTo(final Output output) {
this((Scalar<OutputStream>) output::stream);
}

/**
* Ctor.
* @param tgt Target
*/
private OutputStreamTo(final Scalar<OutputStream> tgt) {
super();
this.target = new UncheckedScalar<>(new StickyScalar<>(tgt));
}

@Override
public void write(final int data) throws IOException {
this.target.value().write(data);
}

@Override
public void write(final byte[] buffer) throws IOException {
this.target.value().write(buffer);
}

@Override
public void write(final byte[] buffer, final int offset,
final int length) throws IOException {
this.target.value().write(buffer, offset, length);
}

@Override
public void close() throws IOException {
this.target.value().close();
}

}
9 changes: 9 additions & 0 deletions src/main/java/org/cactoos/io/OutputTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.file.Path;
import org.cactoos.Output;

Expand Down Expand Up @@ -63,6 +64,14 @@ public OutputTo(final Path path) {
this(() -> new FileOutputStream(path.toFile()));
}

/**
* Ctor.
* @param writer The writer
*/
public OutputTo(final Writer writer) {
this(new WriterAsOutputStream(writer));
}

/**
* Ctor.
* @param stream The stream
Expand Down
Loading