Reads the user version number out of the database header from the given file. @param databaseFile the database file. @return the database version @throws IOException if something goes wrong reading the file, such as bad database header or missing permissions. @see <a href="https://www.sqlite.org/f
(@NonNull File databaseFile)
| 156 | * Number</a>. |
| 157 | */ |
| 158 | public static int readVersion(@NonNull File databaseFile) throws IOException { |
| 159 | FileChannel input = null; |
| 160 | try { |
| 161 | ByteBuffer buffer = ByteBuffer.allocate(4); |
| 162 | input = new FileInputStream(databaseFile).getChannel(); |
| 163 | input.tryLock(60, 4, true); |
| 164 | input.position(60); |
| 165 | int read = input.read(buffer); |
| 166 | if (read != 4) { |
| 167 | throw new IOException("Bad database header, unable to read 4 bytes at offset 60"); |
| 168 | } |
| 169 | buffer.rewind(); |
| 170 | return buffer.getInt(); // ByteBuffer is big-endian by default |
| 171 | } finally { |
| 172 | if (input != null) { |
| 173 | input.close(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * CancellationSignal is only available from API 16 on. This function will create a new |
no test coverage detected