org.cactoos.scalar.IoChecked Java Examples

The following examples show how to use org.cactoos.scalar.IoChecked. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TempFolder.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public void close() throws IOException {
    new IoCheckedProc<>(
        new ForEach<Path>(
            new ProcOf<>(
                path -> path.toFile().delete()
            )
        )
    ).exec(
        new Sorted<>(
            Comparator.reverseOrder(),
            new Directory(
                new IoChecked<>(this).value()
            )
        )
    );
}
 
Example #2
Source File: RsPrint.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param res Original response
 */
public RsPrint(final Response res) {
    super(res);
    this.text = new IoChecked<>(
        new Sticky<>(
            new Scalar<String>() {
                private final ByteArrayOutputStream baos =
                    new ByteArrayOutputStream();

                @Override
                public String value() throws Exception {
                    RsPrint.this.printHead(this.baos);
                    RsPrint.this.printBody(this.baos);
                    return new TextOf(
                        this.baos.toByteArray()
                    ).asString();
                }
            }
        )
    );
}
 
Example #3
Source File: InputOf.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param src The bytes
 */
public InputOf(final Bytes src) {
    this(
        () -> new IoChecked<InputStream>(
            () -> new ByteArrayInputStream(src.asBytes())
        ).value()
    );
}
 
Example #4
Source File: App.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Copy resource.
 * @param name The name of resource
 * @throws IOException If fails
 */
private void copy(final String name) throws IOException {
    new IoChecked<>(
        new LengthOf(
            new TeeInput(
                new ResourceOf(String.format("org/jpeek/%s", name)),
                this.output.resolve(name)
            )
        )
    ).value();
}
 
Example #5
Source File: App.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Save file.
 * @param data Content
 * @param name The name of destination file
 * @throws IOException If fails
 */
private void save(final String data, final String name) throws IOException {
    new IoChecked<>(
        new LengthOf(
            new TeeInput(
                data,
                this.output.resolve(name)
            )
        )
    ).value();
}
 
Example #6
Source File: Replaced.java    From cactoos with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * <p>
 * The given {@link Pattern regex} is used to produce a
 * {@link Pattern#matcher(java.lang.CharSequence) matcher} that will be
 * transformed by {@code func} into a replacement string to replace each
 * {@link Matcher#find() matching} substring.
 * <p>
 * Example usage:
 * <pre>{@code
 * final String result = new Replaced(
 *      new TextOf("one two THREE four FIVE six"),
 *      () -> Pattern.compile("[a-z]+"),
 *      matcher -> String.valueOf(matcher.group().length())
 * ).asString();  //will return the string "3 3 THREE 4 FIVE 3"
 * }</pre>
 * <p>
 * Note: a {@link PatternSyntaxException} will be thrown if the
 * regular expression's syntax is invalid.
 * @param text The text
 * @param regex The regular expression
 * @param func Transforms the resulting matcher object into a replacement
 *  string. Any exceptions will be wrapped in an {@link IOException}.
 */
public Replaced(
    final Text text,
    final Scalar<Pattern> regex,
    final Func<Matcher, String> func) {
    super((Scalar<String>) () -> {
        final StringBuffer buffer = new StringBuffer();
        final Matcher matcher = new IoChecked<>(regex)
            .value()
            .matcher(text.asString());
        final IoCheckedFunc<Matcher, String> safe =
            new IoCheckedFunc<>(func);
        while (matcher.find()) {
            matcher.appendReplacement(
                buffer,
                safe.apply(matcher)
            );
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    });
}
 
Example #7
Source File: IoCheckedFunc.java    From cactoos with MIT License 4 votes vote down vote up
@Override
public Y apply(final X input) throws IOException {
    return new IoChecked<>(() -> this.func.apply(input)).value();
}
 
Example #8
Source File: IoCheckedBiFunc.java    From cactoos with MIT License 4 votes vote down vote up
@Override
public Z apply(final X first, final Y second) throws IOException {
    return new IoChecked<>(
        () -> this.func.apply(first, second)
    ).value();
}
 
Example #9
Source File: Sticky.java    From cactoos with MIT License 4 votes vote down vote up
@Override
public InputStream stream() throws Exception {
    return new ByteArrayInputStream(
        new IoChecked<>(this.cache).value()
    );
}
 
Example #10
Source File: Reports.java    From jpeek with MIT License 4 votes vote down vote up
@SuppressWarnings("PMD.CyclomaticComplexity")
@Override
public Func<String, Response> apply(final String group,
    final String artifact) throws IOException {
    final String grp = group.replace(".", "/");
    final Path input = this.sources.resolve(grp).resolve(artifact);
    Reports.deleteIfPresent(input);
    final String version = new XMLDocument(
        new TextOf(
            new URL(
                String.format(
                    // @checkstyle LineLength (1 line)
                    "https://repo1.maven.org/maven2/%s/%s/maven-metadata.xml",
                    grp, artifact
                )
            )
        ).asString()
    ).xpath("/metadata/versioning/latest/text()").get(0);
    final String name = String.format("%s-%s.jar", artifact, version);
    new IoChecked<>(
        new LengthOf(
            new TeeInput(
                new URL(
                    String.format(
                        "https://repo1.maven.org/maven2/%s/%s/%s/%s",
                        grp, artifact, version, name
                    )
                ),
                input.resolve(name)
            )
        )
    ).value();
    extractClasses(input.resolve(name));
    final Path output = this.target.resolve(grp).resolve(artifact);
    Reports.deleteIfPresent(output);
    new App(input, output).analyze();
    synchronized (this.sources) {
        new Results().add(String.format("%s:%s", group, artifact), output);
        new Mistakes().add(output);
        new Sigmas().add(output);
    }
    return new TypedPages(new Pages(output));
}
 
Example #11
Source File: FakeBase.java    From jpeek with MIT License 4 votes vote down vote up
@Override
public Iterable<Path> files() throws IOException {
    final Path temp = Files.createTempDirectory("jpeek");
    final Iterable<String> sources = new Mapped<>(
        cls -> String.format("%s.java", cls),
        this.classes
    );
    new IoChecked<>(
        new And(
            java -> {
                new LengthOf(
                    new TeeInput(
                        new ResourceOf(
                            String.format("org/jpeek/samples/%s", java)
                        ),
                        temp.resolve(java)
                    )
                ).value();
            },
            sources
        )
    ).value();
    if (sources.iterator().hasNext()) {
        final int exit;
        try {
            exit = new ProcessBuilder()
                .redirectOutput(ProcessBuilder.Redirect.INHERIT)
                .redirectInput(ProcessBuilder.Redirect.INHERIT)
                .redirectError(ProcessBuilder.Redirect.INHERIT)
                .directory(temp.toFile())
                .command(
                    new ListOf<>(
                        new Joined<String>(
                            new ListOf<>("javac"),
                            sources
                        )
                    )
                )
                .start()
                .waitFor();
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException(ex);
        }
        if (exit != 0) {
            throw new IllegalStateException(
                String.format("javac failed with exit code %d", exit)
            );
        }
    }
    return new DefaultBase(temp).files();
}
 
Example #12
Source File: TextEnvelope.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param text Text representing the text value.
 */
public TextEnvelope(final Text text) {
    this(new IoChecked<>(text::asString));
}
 
Example #13
Source File: TextEnvelope.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param scalar Scalar representing the text value.
 */
public TextEnvelope(final Scalar<String> scalar) {
    this.origin = new IoChecked<>(scalar);
}
 
Example #14
Source File: InputOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 *
 * @param scalar The url
 */
public InputOf(final Scalar<URL> scalar) {
    this(() -> new IoChecked<>(scalar).value().openStream());
}
 
Example #15
Source File: TempFile.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Deletes the file from the filesystem.
 * @checkstyle NoJavadocForOverriddenMethodsCheck (5 lines)
 * @checkstyle JavadocMethodCheck (5 lines)
 */
@Override
public void close() throws IOException {
    Files.delete(new IoChecked<>(this).value());
}