Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into padded-text-start
Browse files Browse the repository at this point in the history
* upstream/master:
  yegor256#134: UrlAsInput accepts URI and String
  yegor256#132: InputAsLSInput
  yegor256#130: fix
  yegor256#130: reproduced
  yegor256#128: more ctors
  yegor256#126: UncheckedText with callback
  yegor256#126: UncheckedIOException
  yegor256#126: optimized
  yegor256#126: UncheckedBytes with fallback
  • Loading branch information
vivekimsit committed Jun 15, 2017
2 parents 62b87c6 + 0fc720a commit f704c27
Show file tree
Hide file tree
Showing 17 changed files with 1,508 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/func/IoCheckedFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
* Func that doesn't throw checked {@link Exception}, but throws
* {@link java.io.IOException} instead.
* {@link IOException} instead.
*
* <p>There is no thread-safety guarantee.
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/func/UncheckedFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.func;

import java.io.IOException;
import java.io.UncheckedIOException;
import org.cactoos.Func;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ public Y apply(final X input) {
try {
return new IoCheckedFunc<>(this.func).apply(input);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
throw new UncheckedIOException(ex);
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/cactoos/func/UncheckedProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.func;

import org.cactoos.Func;
import org.cactoos.Proc;

/**
Expand Down Expand Up @@ -53,12 +52,7 @@ public UncheckedProc(final Proc<X> prc) {

@Override
public void exec(final X input) {
new UncheckedFunc<>(
(Func<X, Boolean>) arg -> {
this.proc.exec(arg);
return true;
}
).apply(input);
new UncheckedFunc<>(new ProcAsFunc<>(this.proc)).apply(input);
}

}
9 changes: 9 additions & 0 deletions src/main/java/org/cactoos/io/BytesAsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.InputStream;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.text.ArrayAsBytes;
import org.cactoos.text.TextAsBytes;

/**
Expand Down Expand Up @@ -55,6 +56,14 @@ public BytesAsInput(final String text) {
this(new TextAsBytes(text));
}

/**
* Ctor.
* @param bytes The bytes
*/
public BytesAsInput(final byte[] bytes) {
this(new ArrayAsBytes(bytes));
}

/**
* Ctor.
* @param bytes The bytes
Expand Down
148 changes: 148 additions & 0 deletions src/main/java/org/cactoos/io/InputAsLSInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* 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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.cactoos.Input;
import org.cactoos.text.BytesAsText;
import org.w3c.dom.ls.LSInput;

/**
* Input as LSInput.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.6
* @checkstyle AbbreviationAsWordInNameCheck (10 lines)
*/
public final class InputAsLSInput implements LSInput {

/**
* The input.
*/
private final Input input;

/**
* Ctor.
* @param inpt Input
*/
public InputAsLSInput(final Input inpt) {
this.input = inpt;
}

@Override
public Reader getCharacterStream() {
return new InputStreamReader(this.getByteStream());
}

@Override
public void setCharacterStream(final Reader stream) {
// nothing to do
}

@Override
public InputStream getByteStream() {
try {
return this.input.stream();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public void setByteStream(final InputStream stream) {
// nothing to do
}

@Override
public String getStringData() {
try {
return new BytesAsText(new InputAsBytes(this.input)).asString();
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
public void setStringData(final String data) {
// nothing to do
}

@Override
public String getSystemId() {
return "#system";
}

@Override
public void setSystemId(final String sid) {
// nothing to do
}

@Override
public String getPublicId() {
return "#public";
}

@Override
public void setPublicId(final String pid) {
// nothing to do
}

@Override
public String getBaseURI() {
return "#base";
}

@Override
public void setBaseURI(final String uri) {
// nothing to do
}

@Override
public String getEncoding() {
return StandardCharsets.UTF_8.displayName();
}

@Override
public void setEncoding(final String encoding) {
// nothing to do
}

@Override
@SuppressWarnings("PMD.BooleanGetMethodName")
public boolean getCertifiedText() {
return true;
}

@Override
public void setCertifiedText(final boolean text) {
// nothing to do
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/ResourceAsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ResourceAsInput(final String res, final ClassLoader ldr) {
input -> {
throw new IOException(
new FormattedText(
"Resource '%s' was not found",
"Resource \"%s\" was not found",
input
).asString()
);
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/org/cactoos/io/TeeInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
*/
package org.cactoos.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import org.cactoos.Input;
import org.cactoos.Output;

Expand All @@ -49,6 +51,86 @@ public final class TeeInput implements Input {
*/
private final Output target;

/**
* Ctor.
* @param input The source
* @param output The output file
* @since 0.5
*/
public TeeInput(final Path input, final Path output) {
this(new PathAsInput(input), new PathAsOutput(output));
}

/**
* Ctor.
* @param input The source
* @param output The output file
* @since 0.5
*/
public TeeInput(final Path input, final File output) {
this(new PathAsInput(input), new FileAsOutput(output));
}

/**
* Ctor.
* @param input The source
* @param output The output file
* @since 0.5
*/
public TeeInput(final File input, final File output) {
this(new FileAsInput(input), new FileAsOutput(output));
}

/**
* Ctor.
* @param input The source
* @param output The output file
* @since 0.5
*/
public TeeInput(final File input, final Path output) {
this(new FileAsInput(input), new PathAsOutput(output));
}

/**
* Ctor.
* @param input The source
* @param file The output file
* @since 0.5
*/
public TeeInput(final String input, final File file) {
this(new BytesAsInput(input), new FileAsOutput(file));
}

/**
* Ctor.
* @param input The source
* @param file The output file
* @since 0.5
*/
public TeeInput(final String input, final Path file) {
this(new BytesAsInput(input), new PathAsOutput(file));
}

/**
* Ctor.
* @param input The source
* @param file The output file
* @since 0.5
*/
public TeeInput(final byte[] input, final Path file) {
this(new BytesAsInput(input), new PathAsOutput(file));
}

/**
* Ctor.
* @param input The source
* @param output The target
* @since 0.5
*/
public TeeInput(final String input, final Output output) {
this(new BytesAsInput(input), output);
}

/**
* Ctor.
* @param input The source
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/org/cactoos/io/UncheckedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import org.cactoos.Bytes;
import org.cactoos.Func;
import org.cactoos.func.UncheckedFunc;

/**
* Bytes that doesn't throw checked {@link Exception}.
Expand All @@ -43,21 +45,45 @@ public final class UncheckedBytes implements Bytes {
*/
private final Bytes bytes;

/**
* Fallback.
*/
private final Func<IOException, byte[]> fallback;

/**
* Ctor.
* @param bts Encapsulated bytes
*/
public UncheckedBytes(final Bytes bts) {
this(
bts,
error -> {
throw new UncheckedIOException(error);
}
);
}

/**
* Ctor.
* @param bytes Encapsulated bytes
* @param bts Encapsulated bytes
* @param fbk Fallback
* @since 0.5
*/
public UncheckedBytes(final Bytes bytes) {
this.bytes = bytes;
public UncheckedBytes(final Bytes bts,
final Func<IOException, byte[]> fbk) {
this.bytes = bts;
this.fallback = fbk;
}

@Override
public byte[] asBytes() {
byte[] data;
try {
return this.bytes.asBytes();
data = this.bytes.asBytes();
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
data = new UncheckedFunc<>(this.fallback).apply(ex);
}
return data;
}

}
Loading

0 comments on commit f704c27

Please sign in to comment.