From f68d4b8fa8567f73145b8dde109db924b78413ac Mon Sep 17 00:00:00 2001 From: Andreas Fankhauser hiddenalpha.ch Date: Fri, 13 Jan 2023 01:22:26 +0100 Subject: Tinker around for an POSIX file API --- .../main/java/ch/hiddenalpha/xtra4j/file/File.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 xtra4j-misc/src/main/java/ch/hiddenalpha/xtra4j/file/File.java diff --git a/xtra4j-misc/src/main/java/ch/hiddenalpha/xtra4j/file/File.java b/xtra4j-misc/src/main/java/ch/hiddenalpha/xtra4j/file/File.java new file mode 100644 index 0000000..be9110d --- /dev/null +++ b/xtra4j-misc/src/main/java/ch/hiddenalpha/xtra4j/file/File.java @@ -0,0 +1,70 @@ +package ch.hiddenalpha.xtra4j.file; + +import java.io.IOException; + + +public interface File { + + /** + *

Based on + * POSIX fread. Nevertheless there are some differences (keep reading below).

+ * + *

If at least one of 'off', 'size' or 'count' is negative, behavior is + * undefined

+ * + *

If 'size' times 'count' is outside range 0 to INT_MAX, behavior is + * undefined.

+ * + *

If 'off', 'size' and 'count' provocate to write outside bounds of given + * 'buf', behavior is undefined.

+ * + * @param buf + * Buffer where requested data gets written to. MUST NOT be null. + * @param off + * Index of buf where the first byte gets written. + * @param size + * Size in bytes of one element. If you don't care, pass 1. + * @param count + * How many times 'size' bytes get read maximally. + * @return + * Count of read elements. This equals to bytes if 'size' is 1. Be aware + * that in future, this may return negative values. For now those are + * not defined but a caller has to be aware that this may be the case + * in future. + */ + int read(byte[] buf, int off, int size, int count) throws IOException; + + /** + *

Based on + * POSIX fwrite

. + * + *

Caller has to ensure that combination of passed arguments always stay + * inside bounds and especially that 'size' times 'count' does NOT overflow. + * Ignoring those, results in undefined behavior.

+ * + * @param buf + * Buffer holding the data to write. + * @param off + * Index of 'buf' where first byte gets read from. + * @param size + * Size in bytes of one element. If you don't care, pass 1. + * @param count + * How many elements of size 'size' are to read. + * @return + * Count of written elements. This equals to bytes if 'size' is 1. For + * now those are not defined but a caller has to be aware that this may + * be the case in future. + */ + int write(byte[] buf, int off, int size, int count) throws IOException; + + /** + *

Based on + * POSIX fseek.

+ * + * @param offset + * @param whence + * @return + */ + int fseek(long offset, int whence); + +} -- cgit v1.1