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