Skip to content

Instantly share code, notes, and snippets.

@leadVisionary
Created April 22, 2018 00:43
Show Gist options
  • Save leadVisionary/46b1d519ae334988e5554091b7b380b7 to your computer and use it in GitHub Desktop.
Save leadVisionary/46b1d519ae334988e5554091b7b380b7 to your computer and use it in GitHub Desktop.
Why SelfEncapsulation?
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* Appends the entry to the file as GZipped.
*/
final class AppendEntryAsGZip<P> extends WriteToFileTask<P> {
/**
* A log.
*/
private static final Logger LOG = Logger.getLogger(AppendEntryAsGZip.class.getName());
/**
* constructs the task.
*
* @param entry should not be null
* @param toFile should not be null
*/
AppendEntryAsGZip(final P entry, final Path toFile) {
super(entry, toFile);
}
@Override
protected void writeEntryToFile(final Path toFile, final P entry) throws IOException {
final StringBuilder sb = new StringBuilder();
sb.append(slurpGZippedFile(toFile));
sb.append(entry.toString());
writeOutGZippedFile(toFile, sb.toString());
}
/**
* reads in a GZipped file.
* @param toFile the file to be read
* @return the lines of the file
* @throws IOException if something goes wrong
*/
private String slurpGZippedFile(final Path toFile) throws IOException {
if (toFile.toFile().length() == 0) {
return "";
}
try (final FileInputStream fis = new FileInputStream(toFile.toFile());
final GZIPInputStream gis = new GZIPInputStream(fis);
final BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"))) {
return br.lines().collect(Collectors.joining());
} catch (final IOException e) {
LOG.log(Level.SEVERE, "Could not open file " + toFile.toString(), e);
throw e;
}
}
/**
* writes out a string to a file, gzipped.
* @param toFile the file to be written
* @param log the string to write, as gzipped
*/
private void writeOutGZippedFile(final Path toFile, final String log) {
try (final FileOutputStream fos = new FileOutputStream(toFile.toFile());
final GZIPOutputStream gos = new GZIPOutputStream(fos)) {
gos.write(log.getBytes(StandardCharsets.UTF_8));
} catch (final IOException e) {
LOG.log(Level.SEVERE, "Could not write file " + toFile.toString(), e);
}
}
}
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import static org.mockito.BDDMockito.*;
public class AppendEntryAsGZipTest {
@Mock Path toFile;
ServiceQueryLogEntry entry;
AppendEntryAsGZip toTest;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
entry = new ServiceQueryLogEntry.Builder()
.build();
toTest = new AppendEntryAsGZip(entry, toFile);
}
@Test(expected = NullPointerException.class)
public void rejectsNullEntry() {
new AppendEntryAsGZip(null, toFile);
}
@Test(expected = NullPointerException.class)
public void rejectsNullPath() {
new AppendEntryAsGZip(entry, null);
}
@Test
public void canWriteAnEntryToFile() throws IOException {
final File f = mock(File.class);
given(f.getPath()).willReturn("./service_log.2018-04-19");
given(toFile.toFile()).willReturn(f);
// surprising NullPointerException:
// at java.io.File.isInvalid(File.java:187)
// at java.io.FileOutputStream.<init>(FileOutputStream.java:205)
// at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
// at AppendEntryAsGZip.writeOutGZippedFile(AppendEntryAsGZip.java:70)
// at AppendEntryAsGZip.writeEntryToFile(AppendEntryAsGZip.java:40)
// at AppendEntryAsGZipTest.canWriteAnEntryToFile(AppendEntryAsGZipTest.java:42)
toTest.writeEntryToFile(toFile, entry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment