com.googlecode.totallylazy.Files Java Examples

The following examples show how to use com.googlecode.totallylazy.Files. 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: ZipTest.java    From totallylazy with Apache License 2.0 6 votes vote down vote up
@Test
public void canZipAndUnzip() throws Exception {
    File playground = emptyVMDirectory("totallylazy");
    File a = file(playground, "a.txt");
    File b = file(playground, "folder/b.txt");
    File zipFile = temporaryFile();

    Zip.zip(playground, zipFile);

    Files.deleteFiles(playground);
    assertThat(a.exists(), is(false));
    assertThat(b.exists(), is(false));

    Zip.unzip(zipFile, playground);

    assertThat(a.exists(), is(true));
    assertThat(b.exists(), is(true));

    Files.deleteFiles(playground);
    zipFile.delete();
}
 
Example #2
Source File: ZipTest.java    From totallylazy with Apache License 2.0 6 votes vote down vote up
@Test
public void preservesModifiedDate() throws Exception {
    File playground = emptyVMDirectory("totallylazy");
    File a = file(playground, "a.txt");
    Date date = date(2001, 1, 10);
    a.setLastModified(date.getTime());
    File zipFile = temporaryFile();

    Zip.zip(playground, zipFile);
    Files.deleteFiles(playground);
    Zip.unzip(zipFile, playground);

    assertThat(a.exists(), is(true));
    assertThat(new Date(a.lastModified()), is(date));

    Files.deleteFiles(playground);
    zipFile.delete();
}
 
Example #3
Source File: WebConsoleResource.java    From java-repl with Apache License 2.0 6 votes vote down vote up
@POST
@Hidden
@Path("snap")
@Produces(MediaType.APPLICATION_JSON)
public Response snap(@FormParam("id") String id) {
    Option<WebConsoleClientHandler> clientHandler = agent.client(id);

    if (!clientHandler.isEmpty()) {
        String snapId = UUID.randomUUID().toString();

        Files.write(clientHandler.get().history().toString("\n").getBytes(), snapFile(snapId));

        return ok()
                .entity(emptyMap(String.class, Object.class)
                        .insert("snap", snapId)
                        .insert("uri", snapUri(snapId).toString())
                );

    } else {
        return response(BAD_REQUEST);
    }
}
 
Example #4
Source File: FileRenderer.java    From yatspec with Apache License 2.0 5 votes vote down vote up
@Override
public void complete(File yatspecOutputDir, Result result) throws Exception {
    final File output = outputFile(yatspecOutputDir, result);
    output.delete();
    output.getParentFile().mkdirs();
    String content = render(result);
    Files.write(content.getBytes("UTF-8"), output);
    System.out.println("Yatspec output:\n" + output);
}
 
Example #5
Source File: TreeVisualiser.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
private void render(TreeMap<?, ?> map) {
    final File file = new File(Files.temporaryDirectory(), getClass().getSimpleName() + ".html");
    Files.write(bytes("<html><head><style>" +
            ".tree { border: 1px solid grey; padding: 0 1px; } " +
            ".key { text-align: center; } " +
            ".tree, .left, .right { display: table-cell; }" +
            "</style></head><body>" + new TreeMapRenderer().render(map) + "</body></html>"), file);
    System.out.println("tree = " + file);
}
 
Example #6
Source File: ConsoleHistory.java    From java-repl with Apache License 2.0 5 votes vote down vote up
public boolean save() {
    if (file.isEmpty())
        return false;

    try {
        Files.write(history.toString("\n").getBytes(), file.get());
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
Example #7
Source File: ConsoleHistoryTest.java    From java-repl with Apache License 2.0 5 votes vote down vote up
@Test
public void savesAndLoadsMultilineHistory() {
    File historyFile = Files.temporaryFile();
    ConsoleHistory history = emptyHistory(never(), some(historyFile))
            .add("multiline\nexpression")
            .add("single line expression")
            .add("another\nmultiline\nexpression");

    history.save();

    assertThat(historyFromFile(never(), some(historyFile)).items(), is(history.items().map(replaceAll("\n", " "))));
}
 
Example #8
Source File: FileSource.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static FileSource fileSource(File folder) {
    return fileSource(folder, Files.recursiveFilesDirectoriesFirst(folder));
}
 
Example #9
Source File: FileSource.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static Function1<File, Pair<String, File>> relativeTo(final File folder) {
    return file -> Pair.pair(Files.relativePath(folder, file), file);
}