| 1 | /* |
| 2 | * Copyright (C) 2006 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * |
| 14 | * Author: Steve Ratcliffe |
| 15 | * Create date: 07-Dec-2006 |
| 16 | */ |
| 17 | package uk.me.parabola.imgfmt.app; |
| 18 | |
| 19 | import java.io.Closeable; |
| 20 | import java.io.IOException; |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | /** |
| 24 | * Interface for writing structured data to an img file. |
| 25 | * |
| 26 | * Implementations will have a constructor that passes in a file channel that will eventually |
| 27 | * be written to. If the output is being buffered, then it should be written on a call to sync(). |
| 28 | * |
| 29 | * @author Steve Ratcliffe |
| 30 | */ |
| 31 | public interface ImgFileWriter extends Closeable { |
| 32 | /** |
| 33 | * Called to write out any saved buffers. The strategy may write |
| 34 | * directly to the file in which case this would have nothing or |
| 35 | * little to do. |
| 36 | * @throws IOException If there is an error writing. |
| 37 | */ |
| 38 | public void sync() throws IOException; |
| 39 | |
| 40 | /** |
| 41 | * Get the position. Needed because may not be reflected in the underlying |
| 42 | * file if being buffered. |
| 43 | * |
| 44 | * @return The logical position within the file. |
| 45 | */ |
| 46 | public int position(); |
| 47 | |
| 48 | /** |
| 49 | * Set the position of the file. |
| 50 | * @param pos The new position in the file. |
| 51 | */ |
| 52 | void position(long pos); |
| 53 | |
| 54 | /** |
| 55 | * Write out a single byte. |
| 56 | * |
| 57 | * @param b The byte to write. |
| 58 | */ |
| 59 | public void put(byte b); |
| 60 | |
| 61 | /** |
| 62 | * Write out int in range -128..127 as single byte. |
| 63 | * @param val The byte to write. |
| 64 | */ |
| 65 | public void put1s(int val); |
| 66 | |
| 67 | /** |
| 68 | * Write out int in range -32768..32767 as two bytes in little endian byte order. |
| 69 | * @param val The value to write. |
| 70 | */ |
| 71 | public void put2s(int val); |
| 72 | |
| 73 | /** |
| 74 | * Write out int in range -0x800000..0x7fffff in little endian byte order. |
| 75 | * @param val The value to write. |
| 76 | */ |
| 77 | public void put3s(int val); |
| 78 | |
| 79 | // don't think needed: |
| 80 | // public void putNs(int nBytes, int val); |
| 81 | |
| 82 | /** |
| 83 | * Write out int in range 0..255 as single byte. |
| 84 | * @param val The value to write. |
| 85 | */ |
| 86 | public void put1u(int val); |
| 87 | |
| 88 | /** |
| 89 | * Write out int in range 0..65535 as two bytes in little endian byte order. |
| 90 | * @param val The value to write. |
| 91 | */ |
| 92 | public void put2u(int val); |
| 93 | |
| 94 | /** |
| 95 | * Write out int in range 0..0xffffff as three bytes in little endian byte order. |
| 96 | * @param val The value to write. |
| 97 | */ |
| 98 | public void put3u(int val); |
| 99 | |
| 100 | /** |
| 101 | * Write out int as 1-4 bytes in little endian byte order. |
| 102 | * verifies that fits OK as unsigned. |
| 103 | * |
| 104 | * @param nBytes The number of bytes to write. |
| 105 | * @param val The value to write. |
| 106 | */ |
| 107 | public void putNu(int nBytes, int val); |
| 108 | |
| 109 | /** |
| 110 | * Write out 4 byte value in. |
| 111 | * @param val The value to write. |
| 112 | */ |
| 113 | public void put4(int val); |
| 114 | |
| 115 | /** |
| 116 | * Write out an arbitrary length sequence of bytes. |
| 117 | * |
| 118 | * @param val The values to write. |
| 119 | */ |
| 120 | public void put(byte[] val); |
| 121 | |
| 122 | /** |
| 123 | * Write out part of a byte array. |
| 124 | * |
| 125 | * @param src The array to take bytes from. |
| 126 | * @param start The start position. |
| 127 | * @param length The number of bytes to write. |
| 128 | */ |
| 129 | public void put(byte[] src, int start, int length); |
| 130 | |
| 131 | /** |
| 132 | * Write out a complete byte buffer. |
| 133 | * |
| 134 | * @param src The buffer to write. |
| 135 | */ |
| 136 | public void put(ByteBuffer src); |
| 137 | |
| 138 | /** |
| 139 | * Returns the size of the file. |
| 140 | * |
| 141 | * Note that this is not a general purpose routine and it may not be |
| 142 | * possible to give the correct answer at all times. |
| 143 | * |
| 144 | * @return The file size in bytes. |
| 145 | */ |
| 146 | public long getSize(); |
| 147 | } |