| | |
| | |
| | |
| | |
| |
|
| | package DafnyLibraries; |
| |
|
| | import java.io.IOException; |
| | import java.io.PrintWriter; |
| | import java.io.StringWriter; |
| | import java.nio.file.Files; |
| | import java.nio.file.Path; |
| | import java.nio.file.Paths; |
| |
|
| | import dafny.DafnySequence; |
| | import dafny.Tuple2; |
| | import dafny.Tuple3; |
| | import dafny.TypeDescriptor; |
| |
|
| | public class FileIO { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public static Tuple3<Boolean, DafnySequence<? extends Byte>, DafnySequence<? extends Character>> |
| | INTERNAL_ReadBytesFromFile(DafnySequence<? extends Character> path) { |
| | try { |
| | final Path pathObj = dafnyStringToPath(path); |
| | final DafnySequence<Byte> readBytes = DafnySequence.fromBytes(Files.readAllBytes(pathObj)); |
| | return Tuple3.create(false, readBytes, DafnySequence.empty(TypeDescriptor.CHAR)); |
| | } catch (Exception ex) { |
| | return Tuple3.create(true, DafnySequence.empty(TypeDescriptor.BYTE), stackTraceToDafnyString(ex)); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public static Tuple2<Boolean, DafnySequence<? extends Character>> |
| | INTERNAL_WriteBytesToFile(DafnySequence<? extends Character> path, DafnySequence<? extends Byte> bytes) { |
| | try { |
| | final Path pathObj = dafnyStringToPath(path); |
| | createParentDirs(pathObj); |
| |
|
| | |
| | @SuppressWarnings("unchecked") |
| | final byte[] byteArr = DafnySequence.toByteArray((DafnySequence<Byte>) bytes); |
| |
|
| | Files.write(pathObj, byteArr); |
| | return Tuple2.create(false, DafnySequence.empty(TypeDescriptor.CHAR)); |
| | } catch (Exception ex) { |
| | return Tuple2.create(true, stackTraceToDafnyString(ex)); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | private static final Path dafnyStringToPath(final DafnySequence<? extends Character> path) { |
| | return Paths.get(new String((char[]) path.toArray().unwrap())); |
| | } |
| |
|
| | |
| | |
| | |
| | private static final void createParentDirs(final Path path) throws IOException { |
| | final Path parentDir = path.toAbsolutePath().getParent(); |
| | if (parentDir == null) { |
| | return; |
| | } |
| | Files.createDirectories(parentDir); |
| | } |
| |
|
| | private static final DafnySequence<Character> stackTraceToDafnyString(final Throwable t) { |
| | final StringWriter stringWriter = new StringWriter(); |
| | final PrintWriter printWriter = new PrintWriter(stringWriter); |
| | t.printStackTrace(printWriter); |
| | final String str = stringWriter.toString(); |
| | return DafnySequence.of(str.toCharArray()); |
| | } |
| | } |
| |
|