Created
December 21, 2018 08:53
-
-
Save FauxFaux/d730709877b0d6596fcd2be3de4e8910 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
public class A { | |
static long checkHeader(String path) throws IOException { | |
// can't use try() as there's no way to catch only the initialisation exception? | |
final FileInputStream fis; | |
try { | |
fis = new FileInputStream(path); | |
} catch (IOException e) { | |
throw new AppEx("opening", e); | |
} | |
try { | |
final byte[] buf = new byte[16]; | |
if (buf.length != fis.read(buf)) { | |
throw new AppEx("reading header", new IOException("short read")); | |
} | |
return buf[2] * buf[14]; | |
} finally { | |
try { | |
fis.close(); | |
} catch (IOException e) { | |
throw new Error("PANIC"); | |
} | |
} | |
} | |
public static void main(String[] args) { | |
if (args.length < 1) { | |
throw new AppEx("type argument required"); | |
} | |
if (!args[0].equals("run")) { | |
throw new AppEx("only the 'run' mode is supported"); | |
} | |
long totals = 0; | |
for (String path : new String[] { "foo.bin", "bar.bin" }) { | |
try { | |
totals += checkHeader(path); | |
} catch (Exception e) { | |
throw new AppEx("checking " + path, e); | |
} | |
} | |
System.out.println("totals: " + totals); | |
} | |
static class AppEx extends RuntimeException { | |
AppEx(String message, Throwable cause) { | |
super(message, cause); | |
} | |
AppEx(String message) { | |
super(message); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate failure; | |
use std::env; | |
use std::fs; | |
use std::io::Read; | |
use failure::ensure; | |
use failure::err_msg; | |
use failure::format_err; | |
use failure::Error; | |
use failure::ResultExt; | |
fn check_header(path: &str) -> Result<u64, Error> { | |
let mut f = fs::File::open(path).with_context(|_| err_msg("opening"))?; | |
let mut buf = [0u8; 16]; | |
f.read_exact(&mut buf) | |
.with_context(|_| err_msg("reading header"))?; | |
Ok(u64::from(buf[2] * buf[14])) | |
} | |
fn main() -> Result<(), Error> { | |
let mode = env::args() | |
.nth(1) | |
.ok_or_else(|| err_msg("type argument required"))?; | |
ensure!("run" == mode, "only the 'run' mode is supported"); | |
let mut totals = 0; | |
for path in &["foo.bin", "bar.bin"] { | |
totals += check_header(path).with_context(|_| format_err!("checking {:?}", path))?; | |
} | |
println!("totals: {totals}", totals = totals); | |
Ok(()) | |
} | |
fn bonus() -> Result<u64, Error> { | |
Ok(["foo.bin", "bar.bin"] | |
.into_iter() | |
.map(|path| -> Result<u64, Error> { | |
Ok(check_header(path).with_context(|_| format_err!("checking {:?}", path))?) | |
}) | |
.collect::<Result<Vec<_>, Error>>() | |
.with_context(|_| err_msg("processing inputs"))? | |
.into_iter() | |
.sum()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment