org.cactoos.io.TeeInput Java Examples

The following examples show how to use org.cactoos.io.TeeInput. 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: Reports.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Extract classes from passed Jar file.
 * @param path Jar file path
 * @throws IOException If fails
 */
private static void extractClasses(final Path path) throws IOException {
    try (JarFile jar = new JarFile(path.toFile())) {
        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            final Path item = path.getParent().resolve(entry.getName());
            if (entry.isDirectory()) {
                item.toFile().mkdir();
                continue;
            }
            final Path parent = item.getParent();
            if (!parent.toFile().exists()) {
                parent.toFile().mkdirs();
            }
            new LengthOf(
                new TeeInput(
                    new InputOf(jar.getInputStream(entry)),
                    item
                )
            ).intValue();
        }
    }
}
 
Example #2
Source File: CompileMojoTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Main can print a simple text.
 * @throws Exception If some problem inside
 */
public void testSimpleCompilation() throws Exception {
    final CompileMojo mojo = new CompileMojo();
    this.temp.create();
    final File src = this.temp.newFolder();
    this.setVariableValueToObject(mojo, "sourceDirectory", src);
    new LengthOf(
        new TeeInput(
            new InputOf(
                "type Pixel:\n  Pixel moveTo(Integer x, Integer y)"
            ),
            new OutputTo(new File(src, "main.eo"))
        )
    ).value();
    final File target = this.temp.newFolder();
    this.setVariableValueToObject(mojo, "targetDirectory", target);
    this.setVariableValueToObject(mojo, "project", new MavenProjectStub());
    mojo.execute();
    MatcherAssert.assertThat(
        new File(target, "Pixel.java").exists(),
        Matchers.is(true)
    );
}
 
Example #3
Source File: BodyUrlTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * Body.URL can provide the expected input.
 * @throws Exception If there is some problem inside.
 */
@Test
public void returnsCorrectInputWithUrl() throws Exception {
    try (TempFile file = new TempFile()) {
        final Bytes body = new BytesOf("URL returnsCorrectInput!");
        try (OutputStream outStream =
            new OutputTo(file.value()).stream()) {
            new LengthOf(
                new TeeInput(body, new OutputTo(outStream))
            ).intValue();
        }
        final Body.Url input = new Body.Url(file.value().toUri().toURL());
        MatcherAssert.assertThat(
            "Body content of Body.Url doesn't provide the correct bytes",
            new BytesOf(input).asBytes(),
            new IsEqual<>(body.asBytes())
        );
    }
}
 
Example #4
Source File: BodyUrlTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * Body.URL can provide the expected length.
 * @throws Exception If there is some problem inside.
 */
@Test
public void returnsCorrectLengthWithUrl() throws Exception {
    try (TempFile file = new TempFile()) {
        final Bytes body = new BytesOf("URL returnsCorrectLength!");
        try (OutputStream outStream =
            new OutputTo(file.value()).stream()) {
            new LengthOf(
                new TeeInput(body, new OutputTo(outStream))
            ).intValue();
        }
        MatcherAssert.assertThat(
            "Body content of Body.Url doesn't have the correct length",
            new LengthOf(
                new Body.Url(file.value().toUri().toURL())
            ).intValue(),
            new IsEqual<>(body.asBytes().length)
        );
    }
}
 
Example #5
Source File: XslReport.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Save report.
 * @param target Target dir
 * @throws IOException If fails
 */
@SuppressWarnings("PMD.GuardLogStatement")
public void save(final Path target) throws IOException {
    final long start = System.currentTimeMillis();
    final XML xml = new StrictXML(
        new ReportWithStatistics(
            this.post.transform(this.xml())
        ),
        XslReport.SCHEMA
    );
    new LengthOf(
        new TeeInput(
            xml.toString(),
            target.resolve(
                String.format("%s.xml", this.metric)
            )
        )
    ).intValue();
    new LengthOf(
        new TeeInput(
            XslReport.STYLESHEET.transform(xml).toString(),
            target.resolve(
                String.format("%s.html", this.metric)
            )
        )
    ).intValue();
    Logger.debug(
        this, "%s.xml generated in %[ms]s",
        this.metric, System.currentTimeMillis() - start
    );
}
 
Example #6
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 #7
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 #8
Source File: HttpServletResponseFake.java    From takes with MIT License 5 votes vote down vote up
@Override
public ServletOutputStream getOutputStream() throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new LengthOf(
        new TeeInput(
            new InputOf(this.response.get().body()),
            new OutputTo(baos)
        )
    ).intValue();
    return new ServletOutputStreamTo(baos);
}
 
Example #9
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 #10
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 #11
Source File: Program.java    From eo with MIT License 4 votes vote down vote up
/**
 * Compile it to Java and save.
 *
 * @throws IOException If fails
 */
public void compile() throws IOException {
    final String[] lines = new TextOf(this.input).asString().split("\n");
    final ANTLRErrorListener errors = new BaseErrorListener() {
        // @checkstyle ParameterNumberCheck (10 lines)
        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer,
            final Object symbol, final int line,
            final int position, final String msg,
            final RecognitionException error) {
            throw new CompileException(
                String.format(
                    "[%d:%d] %s: \"%s\"",
                    line, position, msg, lines[line - 1]
                ),
                error
            );
        }
    };
    final org.eolang.compiler.ProgramLexer lexer =
        new org.eolang.compiler.ProgramLexer(
            CharStreams.fromStream(this.input.stream())
        );
    lexer.removeErrorListeners();
    lexer.addErrorListener(errors);
    final org.eolang.compiler.ProgramParser parser =
        new org.eolang.compiler.ProgramParser(
            new CommonTokenStream(lexer)
        );
    parser.removeErrorListeners();
    parser.addErrorListener(errors);
    final Tree tree = parser.program().ret;
    new IoCheckedScalar<>(
        new And(
            path -> {
                new LengthOf(
                    new TeeInput(
                        path.getValue(),
                        this.target.apply(path.getKey())
                    )
                ).value();
            },
            tree.java().entrySet()
        )
    ).value();
}