View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.geometry.io;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.RandomAccessFile;
21  import java.nio.MappedByteBuffer;
22  import java.nio.channels.FileChannel;
23  
24  /**
25   * This class provides methods to access file data at random positions using
26   * memory mapping for faster data access.
27   */
28  @SuppressWarnings("DuplicatedCode")
29  public class MappedFileReaderAndWriter extends AbstractFileReaderAndWriter {
30  
31      /**
32       * Underlying input file.
33       */
34      private final RandomAccessFile randomAccessFile;
35  
36      /**
37       * Buffer where data is stored.
38       */
39      private final MappedByteBuffer buffer;
40  
41      /**
42       * Internal value indicating if read has been produced.
43       */
44      private boolean read;
45  
46      /**
47       * Constructor.
48       *
49       * @param f    file to read from or write to.
50       * @param mode file opening mode (read only or read write).
51       * @throws IOException if an I/O error occurs or the file is larger than
52       *                     2 GB (the maximum size that can be memory
53       *                     mapped). Use {@link FileReaderAndWriter} to
54       *                     stream larger files.
55       */
56      public MappedFileReaderAndWriter(final File f, final FileChannel.MapMode mode) throws IOException {
57          if (mode == FileChannel.MapMode.READ_ONLY) {
58              this.randomAccessFile = new RandomAccessFile(f, "r");
59          } else {
60              this.randomAccessFile = new RandomAccessFile(f, "rw");
61          }
62          final var length = randomAccessFile.length();
63          if (length > Integer.MAX_VALUE) {
64              // memory mapped buffers are limited to 2 GB
65              randomAccessFile.close();
66              throw new IOException("File is too large to be memory mapped "
67                      + "(limit is " + Integer.MAX_VALUE
68                      + " bytes). Use FileReaderAndWriter to stream it "
69                      + "instead");
70          }
71          try {
72              this.buffer = randomAccessFile.getChannel().map(mode, 0, length);
73              this.buffer.load();
74          } catch (final IOException | RuntimeException e) {
75              // do not leak file descriptor if file cannot be mapped
76              randomAccessFile.close();
77              throw e;
78          }
79      }
80  
81      /**
82       * Reads one byte at current file position and advances one position.
83       *
84       * @return Next byte of data or -1 if end of file is reached.
85       * @throws IOException if an I/O error occurs. Not thrown if end-of-file has
86       *                     been reached.
87       */
88      @Override
89      public int read() throws IOException {
90          if (isEndOfStream()) {
91              return -1;
92          }
93          int value = buffer.get();
94          value = ((value << 8) >> 8) & 0xff;
95          read = true;
96          return value;
97      }
98  
99      /**
100      * Reads up to b.length bytes of data from this file into an array of bytes.
101      * This method blocks until at least one byte of input is available.
102      *
103      * @param b The buffer into which the data is read.
104      * @return The total number of bytes read into the buffer, or -1 if there is
105      * no more data because the end of the file has been reached.
106      * @throws IOException If the first byte cannot be read for any reason other
107      *                     than end of file, or if the file has been closed, or if some other I/O
108      *                     error occurs.
109      */
110     @Override
111     public int read(final byte[] b) throws IOException {
112         if (isEndOfStream()) {
113             return -1;
114         }
115         final var length = Math.min(buffer.remaining(), b.length);
116         if (length > 0) {
117             buffer.get(b, 0, length);
118         }
119         read = true;
120         return length;
121     }
122 
123     /**
124      * Reads up to len bytes of data from this file into an array of bytes. This
125      * method blocks until at least one byte of input is available.
126      * This method behaves in exactly the same way as the
127      * InputStream.read(byte[], int, int) method of InputStream.
128      *
129      * @param b   the buffer into which the data is read.
130      * @param off the start offset in array b at which the data is written.
131      * @param len the maximum number of bytes read.
132      * @return the total number of bytes read into the buffer, or -1 if there is
133      * no more data because the end of the file has been reached.
134      * @throws IOException If the first byte cannot be read for any reason other
135      *                     than end of file, or if the random access file has been closed, or if
136      *                     some other I/O error occurs.
137      */
138     @Override
139     public int read(final byte[] b, final int off, final int len) throws IOException {
140         if (isEndOfStream()) {
141             return -1;
142         }
143         final var length = Math.min(buffer.remaining(), len);
144         if (length > 0) {
145             buffer.get(b, off, length);
146         }
147         read = true;
148         return length;
149     }
150 
151     /**
152      * Attempts to skip over n byte of input discarding the skipped bytes.
153      * <p>
154      * This method may skip over some number of bytes, possibly zero. This may
155      * result from any of a number of conditions; reaching end of file before n
156      * bytes have been skipped is only one possibility. The actual number of
157      * bytes skipped is returned. If n is negative, no bytes are skipped.
158      *
159      * @param n the number of bytes to be skipped.
160      * @return the actual number of bytes skipped.
161      */
162     @Override
163     public long skip(final long n) {
164         if (n < 0) {
165             return 0;
166         }
167 
168         final var skipped = (int) Math.min(buffer.remaining(), n);
169         final var newPos = buffer.position() + skipped;
170         buffer.position(newPos);
171         read = true;
172         return skipped;
173     }
174 
175     /**
176      * Writes the specified byte to this file. The write starts at the current
177      * file pointer.
178      *
179      * @param b the byte to be written.
180      * @throws IOException if an I/O error occurs.
181      */
182     @Override
183     public void write(final int b) throws IOException {
184         randomAccessFile.write(b);
185         read = false;
186     }
187 
188     /**
189      * Writes b.length bytes from the specified byte array to this file,
190      * starting at the current file pointer.
191      *
192      * @param b the data.
193      * @throws IOException if an I/O error occurs.
194      */
195     @Override
196     public void write(final byte[] b) throws IOException {
197         randomAccessFile.write(b);
198         read = false;
199     }
200 
201     /**
202      * Writes len bytes from the specified byte array starting at offset off to
203      * this file.
204      *
205      * @param b   the data.
206      * @param off the start offset in the data.
207      * @param len the number of bytes to write.
208      * @throws IOException if an I/O error occurs.
209      */
210     @Override
211     public void write(final byte[] b, final int off, final int len) throws IOException {
212         randomAccessFile.write(b, off, len);
213         read = false;
214     }
215 
216     /**
217      * Returns the current offset in this file.
218      *
219      * @return the offset from the beginning of the file, in bytes, at which the
220      * next read or write occurs.
221      * @throws IOException in an I/O error occurs.
222      */
223     @Override
224     public long getPosition() throws IOException {
225         if (read) {
226             return buffer.position();
227         } else {
228             return randomAccessFile.getFilePointer();
229         }
230     }
231 
232     /**
233      * Determines whether end of file has been reached (next read() will return
234      * -1). or not.
235      *
236      * @return True if end of file has been reached, false otherwise.
237      * @throws IOException if an I/O error occurs..
238      */
239     @Override
240     public boolean isEndOfStream() throws IOException {
241         return getPosition() >= randomAccessFile.length();
242     }
243 
244     /**
245      * Sets the file-pointer offset, measured from the beginning of this file,
246      * at which the next read or write occurs. Setting the offset beyond the end
247      * of the file will raise an exception.
248      *
249      * @param pos the offset position, measured in bytes from the beginning of
250      *            the file, at which to set the file pointer.
251      */
252     @Override
253     public void seek(final long pos) {
254         buffer.position((int) pos);
255         read = true;
256     }
257 
258     /**
259      * Closes this file stream and releases any system resources associated with
260      * the stream. A closed file cannot perform input or output operations and
261      * cannot be reopened.
262      * If this file has an associated channel then the channel is closed as
263      * well.
264      *
265      * @throws IOException if an I/O error occurs.
266      */
267     @Override
268     public void close() throws IOException {
269         buffer.force();
270         randomAccessFile.close();
271     }
272 
273     /**
274      * Reads a boolean from this file. This method reads a single byte from the
275      * file, starting at the current file pointer. A value of 0 represents
276      * false. Any other value represents true. This method blocks until the byte
277      * is read, the end of the stream is detected, or an exception is thrown.
278      *
279      * @return the boolean value read.
280      */
281     @Override
282     public boolean readBoolean() {
283         read = true;
284         return buffer.get() != 0;
285     }
286 
287     /**
288      * Reads a signed eight-bit value from this file. This method reads a byte
289      * from the file, starting from the current file pointer. If the byte read
290      * is b, where 0 &lt;= b &lt;= 255, then the result is: (byte)(b)
291      * This method blocks until the byte is read, the end of the stream is
292      * detected, or an exception is thrown.
293      *
294      * @return the next byte of this file is a signed eight-bit byte.
295      */
296     @Override
297     public byte readByte() {
298         read = true;
299         return buffer.get();
300     }
301 
302     /**
303      * Reads an unsigned eight-bit number from this file. This method reads a
304      * byte from this file, starting at the current file pointer, and returns
305      * that byte.
306      * This method blocks until the byte is read, the end of the stream is
307      * detected, or an exception is thrown.
308      *
309      * @return the next byte of this file, interpreted as an unsigned eight-bit
310      * number.
311      */
312     @Override
313     public short readUnsignedByte() {
314         read = true;
315         return (short) (((buffer.get() << 8) >> 8) & 0xff);
316     }
317 
318     /**
319      * Reads a signed 16-bit number from this file. The method reads two byte
320      * from this file, starting at the current file pointer. If the two bytes
321      * read, in order, are b1 and b2, where each of the two values is between 0
322      * and 255, inclusive, then the result is equal to: (short)(b1 &lt;&lt; 8 |
323      * b2).
324      * This method blocks until the two bytes are read, the end of the stream is
325      * detected, or an exception is thrown.
326      *
327      * @return the next two bytes of this file, interpreted as a signed 16-bit
328      * number.
329      */
330     @Override
331     public short readShort() {
332         read = true;
333         return buffer.getShort();
334     }
335 
336     /**
337      * Reads a signed 16-bit number from this file assuming that file is encoded
338      * using provided endian type. If endian type is big endian type, then
339      * natural binary order is preserved, otherwise byte order is reversed.
340      * This method blocks until the two bytes of the 16-bit number are read, the
341      * end of the stream is detected, or an exception is thrown.
342      *
343      * @param endianType Endian type. Big endian preserves natural binary order,
344      *                   little endian reverses byte order.
345      * @return the next two bytes of this file, interpreted as a signed 16-bit
346      * number encoded in provided endian type.
347      */
348     @Override
349     public short readShort(final EndianType endianType) {
350         read = true;
351         return Util.fromEndianType(endianType, buffer.getShort());
352     }
353 
354     /**
355      * Reads an unsigned 16-bit number from this file. This method reads two
356      * bytes from this file, starting at the current file pointer. If the bytes
357      * read, in order, are b1 and b2, where 0 &lt;= b1, b2 &lt;= 255, then the
358      * result is equal to: (b1 &lt;&lt; 8) | b2
359      * This method blocks until the two bytes are read, the end of the stream is
360      * detected, or an exception is thrown.
361      *
362      * @return the next two bytes of this file, interpreted as an unsigned
363      * 16-bit integer.
364      */
365     @Override
366     public int readUnsignedShort() {
367         read = true;
368         final var value = buffer.getShort();
369 
370         // convert value to byte array
371         final var firstShortByte = 0xff & (value >> 8);
372         final var secondShortByte = 0xff & ((value << 8) >> 8);
373 
374         // return it as integer
375         return 0xffff & ((firstShortByte << 8) | secondShortByte);
376     }
377 
378     /**
379      * Reads an unsigned 16-bit number from this file. This method reads two
380      * bytes from this file, starting at the current file pointer and using
381      * provided endian type. If endian type is big endian, then natural binary
382      * order is preserved, otherwise byte order is reversed.
383      * This method blocks until the two bytes are read, the end of the stream is
384      * detected, or an exception is thrown.
385      *
386      * @param endianType Endian type. Big endian preserves natural binary order,
387      *                   little endian reverses byte order.
388      * @return the next two bytes of this file, interpreted as an unsigned
389      * 16-bit integer.
390      */
391     @Override
392     public int readUnsignedShort(final EndianType endianType) {
393         read = true;
394         final var streamValue = buffer.getShort();
395         final var value = Util.fromEndianType(endianType, streamValue);
396 
397         // convert value to byte array
398         final var secondShortByte = 0xff & ((value << 8) >> 8);
399         final var firstShortByte = 0xff & (value >> 8);
400 
401         // return it as integer
402         return 0xffff & ((firstShortByte << 8) | secondShortByte);
403     }
404 
405     /**
406      * Reads a signed 32-bit integer from this file. This method reads 4 bytes
407      * from the file, starting at the current file pointer. If the bytes read,
408      * in order, are b1, b2, b3, and b4, where 0 &lt;= b1, b3, b4 &lt;= 255, then
409      * the result is equal to: (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) +
410      * b4.
411      * This method blocks until the four bytes are read, the end of the stream
412      * is detected, or an exception is thrown.
413      *
414      * @return the next four bytes of this file, interpreted as an int.
415      */
416     @Override
417     public int readInt() {
418         read = true;
419         return buffer.getInt();
420     }
421 
422     /**
423      * Reads a signed 32-bit integer from this file. This method reads 4 bytes
424      * from the file, starting at the current file pointer and using provided
425      * endian type. If endian type is big endian, then natural binary order is
426      * preserved, otherwise byte order is reversed.
427      * This method blocks until the four bytes are read, the end of the stream
428      * is detected, or an exception is thrown.
429      *
430      * @param endianType Endian type. Big endian preserves natural binary order,
431      *                   little endian reverses byte order.
432      * @return the next four bytes of this file, interpreted as an int.
433      */
434     @Override
435     public int readInt(final EndianType endianType) {
436         read = true;
437         return Util.fromEndianType(endianType, buffer.getInt());
438     }
439 
440     /**
441      * Reads an unsigned 32-bit integer from this file. This method reads 4
442      * bytes from the file, starting at the current file pointer. If the bytes
443      * read, in order, are b1, b2, b3, and b4, where 0 &lt;= b1, b3, b4 &lt;= 255,
444      * then the result is equal to: (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) +
445      * (b3 &lt;&lt; 8) + b4.
446      * This method blocks until the four bytes are read, the end of the stream
447      * is detected, or an exception is thrown.
448      *
449      * @return the next four bytes of this file, interpreted as a long.
450      */
451     @Override
452     public long readUnsignedInt() {
453         read = true;
454         final var value = buffer.getInt();
455 
456         // convert value to byte array
457         final var firstIntByte = 0xff & (value >> 24);
458         final var secondIntByte = 0xff & ((value << 8) >> 24);
459         final var thirdIntByte = 0xff & ((value << 16) >> 24);
460         final var fourthIntByte = 0xff & ((value << 24) >> 24);
461 
462         // return it as integer
463         return ((long) firstIntByte << 24) | (secondIntByte << 16) | (thirdIntByte << 8) | fourthIntByte;
464     }
465 
466     /**
467      * Reads an unsigned 32-bit integer from this file. This method reads 4
468      * bytes from the file, starting at the current file pointer and using
469      * provided endian type. If endian type is big endian, then natural binary
470      * order is preserved, otherwise byte order is reversed.
471      * This method blocks until the four bytes are read, the end of the stream
472      * is detected, or an exception is thrown.
473      *
474      * @param endianType Endian type. Big endian preserves natural binary order,
475      *                   little endian reverses byte order.
476      * @return the next four bytes of this file, interpreted as an int.
477      */
478     @Override
479     public long readUnsignedInt(final EndianType endianType) {
480         read = true;
481         final var streamValue = buffer.getInt();
482         final var value = Util.fromEndianType(endianType, streamValue);
483 
484         // convert value to byte array
485         final var firstIntByte = 0xff & (value >> 24);
486         final var secondIntByte = 0xff & ((value << 8) >> 24);
487         final var thirdIntByte = 0xff & ((value << 16) >> 24);
488         final var fourthIntByte = 0xff & ((value << 24) >> 24);
489 
490         // return it as integer
491         return ((long) firstIntByte << 24) | (secondIntByte << 16) | (thirdIntByte << 8) | fourthIntByte;
492     }
493 
494     /**
495      * Reads a signed 64-bit integer from this file. This method reads eight
496      * bytes from the file, starting at the current file pointer. If the bytes
497      * read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where:
498      * 0 &lt;= b1, b2, b3, b4. b5. b6. b7. b8 &lt;= 255,
499      * then the result is equal to:
500      * ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
501      * + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
502      * + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
503      * + ((long)b7 &lt;&lt; 8) + b8
504      * This method blocks until the eight bytes are read, the end of the stream
505      * is detected, or an exception is thrown.
506      *
507      * @return the next eight bytes of this file, interpreted as a long
508      */
509     @Override
510     public long readLong() {
511         read = true;
512         return buffer.getLong();
513     }
514 
515     /**
516      * Reads a signed 64-bit integer from this file. This method reads eight
517      * bytes from the file, starting at the current file pointer and using
518      * provided endian type. If endian type is big endian, then natural binary
519      * order is preserved, otherwise byte order is reversed.
520      * This method blocks until the eight bytes are read, the end of the stream
521      * is detected, or an exception is thrown.
522      *
523      * @param endianType Endian type. Big endian preserves natural binary order,
524      *                   little endian reverses byte order.
525      * @return the next eight bytes of this file, interpreted as a long
526      */
527     @Override
528     public long readLong(final EndianType endianType) {
529         read = true;
530         return Util.fromEndianType(endianType, buffer.getLong());
531     }
532 
533     /**
534      * Reads a float from this file. This method reads an int value, starting at
535      * the current file pointer, as if by the readInt method and then converts
536      * that in to a float using the intBitsToFloat method in class Float.
537      * This method blocks until the four bytes are read, the end of the stream
538      * is detected, or an exception is thrown.
539      *
540      * @return the next four bytes of this file, interpreted as a float.
541      */
542     @Override
543     public float readFloat() {
544         read = true;
545         return buffer.getFloat();
546     }
547 
548     /**
549      * Reads a float from this file. This method reads four bytes using
550      * provided endian type. If endian type is big endian, then natural binary
551      * order is preserved, otherwise byte order is reversed.
552      * This method blocks until the four bytes are read, the end of the stream
553      * is detected, or an exception is thrown.
554      *
555      * @param endianType Endian type. Big endian preserves natural binary order,
556      *                   little endian reverses byte order.
557      * @return the next four bytes of this file, interpreted as a float.
558      */
559     @Override
560     public float readFloat(final EndianType endianType) {
561         read = true;
562         return Util.fromEndianType(endianType, buffer.getFloat());
563     }
564 
565     /**
566      * Reads a double from this file. This method reads a long value, starting
567      * at the current file pointer, as if by the readLong method and then
568      * converts that long to a double using the longBitsToDouble method in class
569      * Double.
570      * This method blocks until the eight bytes are read, the end of the stream
571      * is detected, or an exception is thrown.
572      *
573      * @return the next eight bytes of this file, interpreted as a double.
574      */
575     @Override
576     public double readDouble() {
577         read = true;
578         return buffer.getDouble();
579     }
580 
581     /**
582      * Reads a double from this file. This method reads eight bytes using
583      * provided endian type. If endian type is big endian, then natural binary
584      * order is preserved, otherwise byte order is reversed.
585      * This method blocks until the eight bytes are read, the end of the stream
586      * is detected, or an exception is thrown.
587      *
588      * @param endianType Endian type. Big endian preserves natural binary order,
589      *                   little endian reverses byte order.
590      * @return the next eight bytes of this file, interpreted as a double.
591      */
592     @Override
593     public double readDouble(final EndianType endianType) {
594         read = true;
595         return Util.fromEndianType(endianType, buffer.getDouble());
596     }
597 
598     /**
599      * Reads the next line of text from this file. This method successively
600      * reads bytes from the file, starting at the current file pointer, until it
601      * reaches a line terminator of the end of the file. Each byte is converted
602      * into a character by taking the byte's value for the lower eight bits of
603      * the character and setting the high eight bits of the character to zero.
604      * This method does not, therefore, support the full Unicode character set.
605      * A line of text is terminated by a carriage-return character ('\r'), a
606      * newline character('\n'), a carriage-return character immediately followed
607      * by a newline character, or the end of the file. Line-terminating
608      * characters are discarded and are not included as part of the string
609      * returned.
610      * This method blocks until a newline character is read, a carriage return
611      * and the byte following it are read (to see if it is a newline), the end
612      * of the file is reached, or an exception is thrown.
613      *
614      * @return the next line of text from this file, or null if end of file is
615      * encountered before even one byte is read.
616      */
617     @Override
618     public String readLine() {
619         read = true;
620         if (buffer.hasRemaining()) {
621             return readUntilAnyOfTheseCharactersIsFound("\n");
622         } else {
623             return null;
624         }
625     }
626 
627     /**
628      * Sequentially reads characters starting at current file position until one
629      * of the characters in provided pattern is found.
630      * All characters read so far will be returned without including any of the
631      * pattern characters.
632      *
633      * @param pattern Stop characters to stop reading when they are found.
634      * @return String read so far until any of the pattern characters was found
635      * or an empty string if the first character is contained in provided
636      * pattern.
637      * @throws IllegalArgumentException if no pattern characters are provided.
638      */
639     @Override
640     public String readUntilAnyOfTheseCharactersIsFound(final String pattern) {
641         if (pattern.isEmpty()) {
642             throw new IllegalArgumentException();
643         }
644 
645         read = true;
646 
647         final var stringBuffer = new StringBuilder();
648         final var charBuffer = new byte[1];
649         String character;
650         while (buffer.hasRemaining()) {
651             charBuffer[0] = buffer.get();
652             character = new String(charBuffer);
653             if (pattern.contains(character)) {
654                 // character found
655                 break;
656             }
657             // add character to output buffer
658             stringBuffer.append(character);
659         }
660 
661         return stringBuffer.toString();
662     }
663 
664     /**
665      * Writes a boolean to the file as a one-byte value. The value true is
666      * written out as the value (byte)1; the value false is written out as the
667      * value (byte)0. The write starts at the current position of the file
668      * pointer.
669      *
670      * @param v a boolean value to be written.
671      * @throws IOException if an I/O error occurs.
672      */
673     @Override
674     public void writeBoolean(final boolean v) throws IOException {
675         randomAccessFile.writeBoolean(v);
676         read = false;
677     }
678 
679     /**
680      * Writes a byte to the file as a one-byte value. The write starts at the
681      * current position of the file pointer.
682      *
683      * @param v a byte value to be written.
684      * @throws IOException if an I/O error occurs.
685      */
686     @Override
687     public void writeByte(final byte v) throws IOException {
688         randomAccessFile.writeByte(v);
689         read = false;
690     }
691 
692     /**
693      * Writes provided value in the range 0-255 as an unsigned byte. The write
694      * starts at the current position of the file pointer.
695      *
696      * @param v a value to be written as an unsigned byte.
697      * @throws IOException if an I/O error occurs.
698      */
699     @Override
700     public void writeUnsignedByte(final short v) throws IOException {
701         final var data = (byte) (0xff & v);
702         randomAccessFile.writeByte(data);
703         read = false;
704     }
705 
706     /**
707      * Writes a short to the file as two bytes, high byte first. The write
708      * starts at the current position of the file pointer.
709      *
710      * @param v a short to be written.
711      * @throws IOException if an I/O error occurs.
712      */
713     @Override
714     public void writeShort(final short v) throws IOException {
715         randomAccessFile.writeShort(v);
716         read = false;
717     }
718 
719     /**
720      * Writes a short to the file as two bytes using provided endian type.
721      * If endian type is big endian, then natural byte order is preserved (and
722      * high byte is written first), if little endian order is chosen, then byte
723      * order is reversed
724      *
725      * @param v          a short to be written
726      * @param endianType endian type. If it is big endian, natural byte order is
727      *                   preserved, otherwise byte order is reversed.
728      * @throws IOException if an I/O error occurs.
729      */
730     @Override
731     public void writeShort(final short v, final EndianType endianType) throws IOException {
732         final var value = Util.toEndianType(endianType, v);
733         randomAccessFile.writeShort(value);
734         read = false;
735     }
736 
737     /**
738      * Writes an unsigned short to the file as two bytes, high byte first.
739      * Provided integer value is converted to an unsigned short by taking into
740      * account only the two lower bytes. The write starts at the current
741      * position of the file pointer.
742      *
743      * @param v an unsigned short to be written (int is converted to unsigned
744      *          short).
745      * @throws IOException if an I/O error occurs.
746      */
747     @Override
748     public void writeUnsignedShort(final int v) throws IOException {
749         final var firstShortByte = 0xff & (v >> 8);
750         final var secondShortByte = 0xff & ((v << 8) >> 8);
751 
752         final var value = (short) ((firstShortByte << 8) | secondShortByte);
753 
754         randomAccessFile.writeShort(value);
755         read = false;
756     }
757 
758     /**
759      * Writes an unsigned short to the file as two bytes, using provided endian
760      * type.
761      * Provided integer value is converted to an unsigned short by taking into
762      * account only the two lower bytes.
763      * If endian type is big endian, then natural byte order is preserved (and
764      * high byte is written first), if little endian order is chosen, then byte
765      * order is reversed
766      * The write starts at the current position of the file pointer.
767      *
768      * @param v          an unsigned short to be written (int is converted to unsigned
769      *                   short).
770      * @param endianType endian type. If it is big endian, natural byte order is
771      *                   preserved, otherwise byte order is reversed.
772      * @throws IOException if an I/O error occurs.
773      */
774     @Override
775     public void writeUnsignedShort(final int v, final EndianType endianType) throws IOException {
776         final var firstShortByte = 0xff & (v >> 8);
777         final var secondShortByte = 0xff & ((v << 8) >> 8);
778 
779         final var machineValue = (short) ((firstShortByte << 8) | secondShortByte);
780         final var value = Util.toEndianType(endianType, machineValue);
781 
782         randomAccessFile.writeShort(value);
783         read = false;
784     }
785 
786     /**
787      * Writes an int to the file as four bytes, high byte first. The write
788      * starts at the current position of the file pointer.
789      *
790      * @param v an int to be written.
791      * @throws IOException if an I/O error occurs.
792      */
793     @Override
794     public void writeInt(final int v) throws IOException {
795         randomAccessFile.writeInt(v);
796         read = false;
797     }
798 
799     /**
800      * Writes an int to the file as four bytes, using provided endian type.
801      * If endian type is big endian, then natural byte order is preserved (and
802      * high byte is written first), if little endian order is chosen, then byte
803      * order is reversed.
804      * The write starts at the current position of the file pointer.
805      *
806      * @param v          an int to be written.
807      * @param endianType endian type. If it is big endian, natural byte order is
808      *                   preserved, otherwise byte order is reversed.
809      * @throws IOException if an I/O error occurs.
810      */
811     @Override
812     public void writeInt(final int v, final EndianType endianType) throws IOException {
813         final var value = Util.toEndianType(endianType, v);
814         randomAccessFile.writeInt(value);
815         read = false;
816     }
817 
818     /**
819      * Writes an unsigned int to the file as four bytes, high byte first.
820      * Provided integer value is converted to an unsigned int by taking into
821      * account only the four lower bytes. The write starts at the current
822      * position of the file pointer.
823      *
824      * @param v an unsigned int to be written (long is converted to unsigned
825      *          int).
826      * @throws IOException if an I/O error occurs.
827      */
828     @Override
829     public void writeUnsignedInt(final long v) throws IOException {
830         final var firstIntByte = (int) (0xff & (v >> 24));
831         final var secondIntByte = (int) (0xff & ((v << 8) >> 24));
832         final var thirdIntByte = (int) (0xff & ((v << 16) >> 24));
833         final var fourthIntByte = (int) (0xff & ((v << 24) >> 24));
834 
835         final var value = (firstIntByte << 24) | (secondIntByte << 16) | (thirdIntByte << 8) | fourthIntByte;
836 
837         randomAccessFile.writeInt(value);
838         read = false;
839     }
840 
841     /**
842      * Writes an unsigned int to the file as four bytes, using provided endian
843      * type.
844      * Provided integer value is converted to an unsigned int by taking into
845      * account only the four lower bytes.
846      * If endian type is big endian, then natural byte order is preserved (and
847      * high byte is written first), if little endian order is chosen, then byte
848      * order is reversed.
849      * The write starts at the current position of the file pointer.
850      *
851      * @param v          an unsigned int to be written (long is converted to unsigned
852      *                   short).
853      * @param endianType endian type. If it is big endian, natural byte order is
854      *                   preserved, otherwise byte order is reversed.
855      * @throws IOException if an I/O error occurs.
856      */
857     @Override
858     public void writeUnsignedInt(final long v, final EndianType endianType) throws IOException {
859         final var firstIntByte = (int) (0xff & (v >> 24));
860         final var secondIntByte = (int) (0xff & ((v << 8) >> 24));
861         final var thirdIntByte = (int) (0xff & ((v << 16) >> 24));
862         final var fourthIntByte = (int) (0xff & ((v << 24) >> 24));
863 
864         final var machineValue = (firstIntByte << 24) | (secondIntByte << 16) | (thirdIntByte << 8) | fourthIntByte;
865         final var value = Util.toEndianType(endianType, machineValue);
866 
867         randomAccessFile.writeInt(value);
868         read = false;
869     }
870 
871     /**
872      * Writes a long to the file as eight bytes, high byte first. The write
873      * starts at the current position of the file pointer.
874      *
875      * @param v a long to be written.
876      * @throws IOException if an I/O error occurs.
877      */
878     @Override
879     public void writeLong(final long v) throws IOException {
880         randomAccessFile.writeLong(v);
881         read = false;
882     }
883 
884     /**
885      * Writes a long to the file as eight bytes, using provided endian type.
886      * If endian type is big endian, then natural byte order is preserved (and
887      * high byte is written first), if little endian order is chosen, then byte
888      * order is reversed.
889      * The write starts at the current position of the file pointer.
890      *
891      * @param v          a long to be written.
892      * @param endianType endian type. If it is big endian, natural byte order is
893      *                   preserved, otherwise byte order is reversed.
894      * @throws IOException if an I/O error occurs.
895      */
896     @Override
897     public void writeLong(final long v, final EndianType endianType) throws IOException {
898         final var value = Util.toEndianType(endianType, v);
899         randomAccessFile.writeLong(value);
900         read = false;
901     }
902 
903     /**
904      * Converts the float argument to an int using the floatToIntBits method in
905      * class Float, and then write that int value to the file as a four-byte
906      * quantity, high byte first. The write starts at the current position of
907      * the file pointer.
908      *
909      * @param v a float value to be written
910      * @throws IOException if an I/O error occurs.
911      */
912     @Override
913     public void writeFloat(final float v) throws IOException {
914         randomAccessFile.writeFloat(v);
915         read = false;
916     }
917 
918     /**
919      * Converts the float argument to an int using the floatToIntBits method in
920      * class Float, and then write that int value to the file as a four-byte
921      * quantity, using provided endian type.
922      * If endian type is big endian, then natural byte order is preserved (and
923      * high byte is written first), if little endian order is chosen, then byte
924      * order is reversed.
925      * The write starts at the current position of the file pointer.
926      *
927      * @param v          a float value to be written
928      * @param endianType endian type. If it is big endian, natural byte order is
929      *                   preserved, otherwise byte order is reversed.
930      * @throws IOException if an I/O error occurs.
931      */
932     @Override
933     public void writeFloat(final float v, final EndianType endianType) throws IOException {
934         final var value = Util.toEndianType(endianType, v);
935         randomAccessFile.writeFloat(value);
936         read = false;
937     }
938 
939     /**
940      * Converts the double argument to a long using the doubleToLongBits method
941      * in class Double, and then writes that long value to the file as an eight
942      * byte quantity, high byte first. The write starts at the current position
943      * of the file pointer.
944      *
945      * @param v a double value to be written.
946      * @throws IOException if an I/O error occurs.
947      */
948     @Override
949     public void writeDouble(final double v) throws IOException {
950         randomAccessFile.writeDouble(v);
951         read = false;
952     }
953 
954     /**
955      * Converts the double argument to a long using the doubleToLongBits method
956      * in class Double, and then writes that long value to the file as an eight
957      * byte quantity, using provided endian type.
958      * If endian type is big endian, then natural byte order is preserved (and
959      * high byte is written first), if little endian order is chosen, then byte
960      * order is reversed.
961      * The write starts at the current position of the file pointer.
962      *
963      * @param v          a double value to be written.
964      * @param endianType endian type. If it is big endian, natural byte order is
965      *                   preserved, otherwise byte order is reversed.
966      * @throws IOException if an I/O error occurs.
967      */
968     @Override
969     public void writeDouble(final double v, final EndianType endianType) throws IOException {
970         final var value = Util.toEndianType(endianType, v);
971         randomAccessFile.writeDouble(value);
972         read = false;
973     }
974 
975     /**
976      * Writes the string to the file as a sequence of bytes. Each character in
977      * the string is written out, in sequence, by discarding its high eight
978      * bits. The write starts at the current position of the file pointer.
979      *
980      * @param s a string of bytes to be written.
981      * @throws IOException if an I/O error occurs.
982      */
983     @Override
984     public void writeASCII(final String s) throws IOException {
985         randomAccessFile.writeBytes(s);
986         read = false;
987     }
988 }