Skip to content

Commit 3f2965f

Browse files
committed
[refactor] Fix issues with ByteBuffer vs Buffer API change in Java 9+
1 parent 07f6d1f commit 3f2965f

File tree

13 files changed

+85
-74
lines changed

13 files changed

+85
-74
lines changed

exist-core/src/main/java/org/exist/storage/blob/BlobStoreDumpTool.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import java.io.IOException;
4545
import java.io.PrintStream;
46+
import java.nio.Buffer;
4647
import java.nio.ByteBuffer;
4748
import java.nio.channels.SeekableByteChannel;
4849
import java.nio.file.Files;
@@ -127,19 +128,19 @@ public static void dump(final DigestType digestType, final Path persistentFile,
127128
// write a CSV table header
128129
printStream.println("blobId,referenceCount");
129130

130-
buffer.clear();
131+
((Buffer)buffer).clear();
131132

132133
final byte[] id = new byte[digestBytesLen];
133134

134135
while (channel.read(buffer) > -1) {
135-
buffer.flip();
136+
((Buffer)buffer).flip();
136137
buffer.get(id);
137138
final BlobId blobId = new BlobId(id);
138139
final int count = buffer.getInt();
139140

140141
printStream.println(blobId + "," + count);
141142

142-
buffer.clear();
143+
((Buffer)buffer).clear();
143144
}
144145
}
145146
}
@@ -156,12 +157,12 @@ public static void dump(final DigestType digestType, final Path persistentFile,
156157
*/
157158
private static void dumpFileHeader(final PrintStream printStream, final ByteBuffer buffer, final Path persistentFile,
158159
final SeekableByteChannel channel) throws IOException {
159-
buffer.clear();
160-
buffer.limit(BLOB_STORE_HEADER_LEN);
160+
((Buffer)buffer).clear();
161+
((Buffer)buffer).limit(BLOB_STORE_HEADER_LEN);
161162

162163
channel.read(buffer);
163164

164-
buffer.flip();
165+
((Buffer)buffer).flip();
165166

166167
final boolean validMagic =
167168
buffer.get() == BLOB_STORE_MAGIC_NUMBER[0]

exist-core/src/main/java/org/exist/storage/blob/BlobStoreImpl.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.io.IOException;
6161
import java.io.InputStream;
6262
import java.io.OutputStream;
63+
import java.nio.Buffer;
6364
import java.nio.ByteBuffer;
6465
import java.nio.channels.SeekableByteChannel;
6566
import java.nio.file.Files;
@@ -484,7 +485,7 @@ private void closeAfterRecoveryAttempt() {
484485
*/
485486
private void closeBlobStore() {
486487
if (buffer != null) {
487-
buffer.clear();
488+
((Buffer)buffer).clear();
488489
buffer = null;
489490
}
490491

@@ -563,18 +564,18 @@ private ConcurrentMap<BlobId, BlobReference> compactPersistentReferences(final B
563564
try (final SeekableByteChannel channel = Files.newByteChannel(persistentFile, READ)) {
564565

565566
validateFileHeader(buffer, persistentFile, channel);
566-
buffer.clear();
567+
((Buffer)buffer).clear();
567568

568569
try (final SeekableByteChannel compactChannel = Files.newByteChannel(compactPersistentFile,
569570
CREATE_NEW, APPEND)) {
570571

571572
writeFileHeader(buffer, compactChannel);
572573

573-
buffer.clear();
574+
((Buffer)buffer).clear();
574575

575576
while (channel.read(buffer) > -1) {
576577
final byte[] id = new byte[digestType.getDigestLengthBytes()];
577-
buffer.flip();
578+
((Buffer)buffer).flip();
578579
buffer.get(id);
579580
final BlobId blobId = new BlobId(id);
580581
final int count = buffer.getInt();
@@ -586,11 +587,11 @@ private ConcurrentMap<BlobId, BlobReference> compactPersistentReferences(final B
586587

587588
compactReferences.put(blobId, new BlobReference(count, compactChannel.position()));
588589

589-
buffer.flip();
590+
((Buffer)buffer).flip();
590591
compactChannel.write(buffer);
591592
}
592593

593-
buffer.clear();
594+
((Buffer)buffer).clear();
594595
}
595596
}
596597
}
@@ -617,11 +618,11 @@ private ConcurrentMap<BlobId, BlobReference> compactPersistentReferences(final B
617618
private long writeFileHeader(final ByteBuffer buffer, final SeekableByteChannel channel) throws IOException {
618619
final long start = channel.position();
619620

620-
buffer.clear();
621+
((Buffer)buffer).clear();
621622
writeFileHeader(buffer);
622623

623-
buffer.flip();
624-
buffer.limit(BLOB_STORE_HEADER_LEN);
624+
((Buffer)buffer).flip();
625+
((Buffer)buffer).limit(BLOB_STORE_HEADER_LEN);
625626
channel.write(buffer);
626627

627628
return channel.position() - start;
@@ -648,12 +649,12 @@ private static void writeFileHeader(final ByteBuffer buffer) {
648649
*/
649650
private void validateFileHeader(final ByteBuffer buffer, final Path file, final SeekableByteChannel channel)
650651
throws IOException {
651-
buffer.clear();
652-
buffer.limit(BLOB_STORE_HEADER_LEN);
652+
((Buffer)buffer).clear();
653+
((Buffer)buffer).limit(BLOB_STORE_HEADER_LEN);
653654

654655
channel.read(buffer);
655656

656-
buffer.flip();
657+
((Buffer)buffer).flip();
657658

658659
final boolean validMagic =
659660
buffer.get() == BLOB_STORE_MAGIC_NUMBER[0]
@@ -1216,26 +1217,26 @@ private void undoStoreBlobFile(final BlobId blobId, final String stagedUuid) thr
12161217
* @throws IOException if the blob's reference count cannot be set
12171218
*/
12181219
private void updateBlogRefCount(final BlobId blobId, final int count) throws IOException {
1219-
buffer.clear();
1220-
buffer.limit(digestType.getDigestLengthBytes()); // we are only going to read the BlobIds
1220+
((Buffer)buffer).clear();
1221+
((Buffer)buffer).limit(digestType.getDigestLengthBytes()); // we are only going to read the BlobIds
12211222

12221223
// start immediately after the file header
12231224
channel.position(BLOB_STORE_HEADER_LEN);
12241225

12251226
boolean updatedCount = false;
12261227

12271228
while (channel.read(buffer) > 0) {
1228-
buffer.flip();
1229+
((Buffer)buffer).flip();
12291230
final byte[] id = new byte[digestType.getDigestLengthBytes()];
12301231
buffer.get(id);
12311232
final BlobId readBlobId = new BlobId(id);
12321233

12331234
if (blobId.equals(readBlobId)) {
12341235

1235-
buffer.clear();
1236-
buffer.limit(REFERENCE_COUNT_LEN);
1236+
((Buffer)buffer).clear();
1237+
((Buffer)buffer).limit(REFERENCE_COUNT_LEN);
12371238
buffer.putInt(count);
1238-
buffer.flip();
1239+
((Buffer)buffer).flip();
12391240

12401241
channel.write(buffer);
12411242

@@ -1254,11 +1255,11 @@ private void updateBlogRefCount(final BlobId blobId, final int count) throws IOE
12541255
* the next call to compactPersistentReferences
12551256
*/
12561257
if (!updatedCount) {
1257-
buffer.clear();
1258+
((Buffer)buffer).clear();
12581259
buffer.put(blobId.getId());
12591260
buffer.putInt(count);
12601261

1261-
buffer.flip();
1262+
((Buffer)buffer).flip();
12621263

12631264
channel.write(buffer);
12641265
}
@@ -1617,10 +1618,10 @@ private void writeEntry(final BlobId blobId, final BlobReference blobReference,
16171618

16181619
channel.position(blobReference.persistentOffset);
16191620

1620-
buffer.clear();
1621+
((Buffer)buffer).clear();
16211622
buffer.put(blobId.getId());
16221623
buffer.putInt(newCount);
1623-
buffer.flip();
1624+
((Buffer)buffer).flip();
16241625

16251626
channel.write(buffer);
16261627
}

exist-core/src/main/java/org/exist/storage/journal/Journal.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package org.exist.storage.journal;
2323

2424
import java.io.*;
25+
import java.nio.Buffer;
2526
import java.nio.BufferOverflowException;
2627
import java.nio.ByteBuffer;
2728
import java.nio.channels.FileChannel;
@@ -352,14 +353,14 @@ public synchronized void writeToLog(final Loggable entry) throws JournalExceptio
352353
// TODO(AR) this is needed as the journal is initialised by starting a transaction for loading the SymbolTable... before recovery! which is likely wrong!!! as Recovery Cannot run if the Journal file has been switched!
353354
final long pos = channel != null ? channel.position() : 0;
354355

355-
currentLsn = new Lsn(currentJournalFileNumber, pos + currentBuffer.position() + 1);
356+
currentLsn = new Lsn(currentJournalFileNumber, pos + ((Buffer)currentBuffer).position() + 1);
356357
} catch (final IOException e) {
357358
throw new JournalException("Unable to create LSN for: " + entry.dump());
358359
}
359360
entry.setLsn(currentLsn);
360361

361362
try {
362-
final int currentBufferEntryOffset = currentBuffer.position();
363+
final int currentBufferEntryOffset = ((Buffer)currentBuffer).position();
363364

364365
// write entryHeader
365366
currentBuffer.put(entry.getLogType());
@@ -373,7 +374,7 @@ public synchronized void writeToLog(final Loggable entry) throws JournalExceptio
373374
currentBuffer.putShort((short) (size + LOG_ENTRY_HEADER_LEN));
374375

375376
// write checksum
376-
final long checksum = xxHash64.hash(currentBuffer, currentBufferEntryOffset, currentBuffer.position() - currentBufferEntryOffset, XXHASH64_SEED);
377+
final long checksum = xxHash64.hash(currentBuffer, currentBufferEntryOffset, ((Buffer)currentBuffer).position() - currentBufferEntryOffset, XXHASH64_SEED);
377378
currentBuffer.putLong(checksum);
378379
} catch (final BufferOverflowException e) {
379380
throw new JournalException("Buffer overflow while writing log record: " + entry.dump(), e);
@@ -449,8 +450,8 @@ private void flushBuffer() {
449450
}
450451

451452
try {
452-
if (currentBuffer.position() > 0) {
453-
currentBuffer.flip();
453+
if (((Buffer)currentBuffer).position() > 0) {
454+
((Buffer)currentBuffer).flip();
454455
final int size = currentBuffer.remaining();
455456
while (currentBuffer.hasRemaining()) {
456457
channel.write(currentBuffer);
@@ -461,7 +462,7 @@ private void flushBuffer() {
461462
} catch (final IOException e) {
462463
LOG.warn("Flushing log file failed!", e);
463464
} finally {
464-
currentBuffer.clear();
465+
((Buffer)currentBuffer).clear();
465466
}
466467
}
467468

@@ -599,7 +600,7 @@ static void writeJournalHeader(final SeekableByteChannel channel) throws IOExcep
599600
ByteConversion.shortToByteH(JOURNAL_VERSION, journalVersion, 0);
600601
buf.put(journalVersion);
601602

602-
buf.flip();
603+
((Buffer)buf).flip();
603604
channel.write(buf);
604605
}
605606

exist-core/src/main/java/org/exist/storage/journal/JournalReader.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import javax.annotation.Nullable;
3232
import java.io.IOException;
33+
import java.nio.Buffer;
3334
import java.nio.ByteBuffer;
3435
import java.nio.channels.SeekableByteChannel;
3536
import java.nio.file.Files;
@@ -82,7 +83,7 @@ private void validateJournalHeader(final Path file, final SeekableByteChannel fc
8283
// read the magic number
8384
final ByteBuffer buf = ByteBuffer.allocateDirect(JOURNAL_HEADER_LEN);
8485
fc.read(buf);
85-
buf.flip();
86+
((Buffer)buf).flip();
8687

8788
// check the magic number
8889
final boolean validMagic =
@@ -145,12 +146,12 @@ Loggable previousEntry() throws LogException {
145146

146147
// go back 8 bytes (checksum length) + 2 bytes (backLink length) and read the backLink (2 bytes) of the last entry
147148
fc.position(fc.position() - LOG_ENTRY_CHECKSUM_LEN - LOG_ENTRY_BACK_LINK_LEN);
148-
header.clear().limit(LOG_ENTRY_BACK_LINK_LEN);
149+
((Buffer)header).clear().limit(LOG_ENTRY_BACK_LINK_LEN);
149150
final int read = fc.read(header);
150151
if (read != LOG_ENTRY_BACK_LINK_LEN) {
151152
throw new LogException("Unable to read journal entry back-link!");
152153
}
153-
header.flip();
154+
((Buffer)header).flip();
154155
final short backLink = header.getShort();
155156

156157
// position the channel to the start of the previous entry and mark it
@@ -195,7 +196,7 @@ Loggable readEntry() throws LogException {
195196
final Lsn lsn = new Lsn(fileNumber, fc.position() + 1);
196197

197198
// read the entry header
198-
header.clear();
199+
((Buffer)header).clear();
199200
int read = fc.read(header);
200201
if (read <= 0) {
201202
return null;
@@ -204,19 +205,19 @@ Loggable readEntry() throws LogException {
204205
throw new LogException("Incomplete journal entry header found, expected "
205206
+ LOG_ENTRY_HEADER_LEN + " bytes, but found " + read + " bytes");
206207
}
207-
header.flip();
208+
((Buffer)header).flip();
208209

209210
// prepare the checksum for the header
210211
xxHash64.reset();
211212
if (header.hasArray()) {
212213
xxHash64.update(header.array(), 0, LOG_ENTRY_HEADER_LEN);
213214
} else {
214-
final int mark = header.position();
215-
header.position(0);
215+
final int mark = ((Buffer)header).position();
216+
((Buffer)header).position(0);
216217
final byte buf[] = new byte[LOG_ENTRY_HEADER_LEN];
217218
header.get(buf);
218219
xxHash64.update(buf, 0, LOG_ENTRY_HEADER_LEN);
219-
header.position(mark);
220+
((Buffer)header).position(mark);
220221
}
221222

222223
final byte entryType = header.get();
@@ -239,12 +240,12 @@ Loggable readEntry() throws LogException {
239240
// resize the payload buffer
240241
payload = ByteBuffer.allocateDirect(remainingEntryBytes);
241242
}
242-
payload.clear().limit(remainingEntryBytes);
243+
((Buffer)payload).clear().limit(remainingEntryBytes);
243244
read = fc.read(payload);
244245
if (read < remainingEntryBytes) {
245246
throw new LogException("Incomplete log entry found!");
246247
}
247-
payload.flip();
248+
((Buffer)payload).flip();
248249

249250
// read entry data
250251
loggable.read(payload);
@@ -260,12 +261,12 @@ Loggable readEntry() throws LogException {
260261
if (payload.hasArray()) {
261262
xxHash64.update(payload.array(), 0, size + LOG_ENTRY_BACK_LINK_LEN);
262263
} else {
263-
final int mark = payload.position();
264-
payload.position(0);
264+
final int mark = ((Buffer)payload).position();
265+
((Buffer)payload).position(0);
265266
final byte buf[] = new byte[size + LOG_ENTRY_BACK_LINK_LEN];
266267
payload.get(buf);
267268
xxHash64.update(buf, 0, size + LOG_ENTRY_BACK_LINK_LEN);
268-
payload.position(mark);
269+
((Buffer)payload).position(mark);
269270
}
270271

271272
// read the entry checksum

exist-core/src/main/java/org/exist/storage/lock/FileLock.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.exist.util.ReadOnlyException;
2929

3030
import java.io.IOException;
31+
import java.nio.Buffer;
3132
import java.nio.ByteBuffer;
3233
import java.nio.channels.SeekableByteChannel;
3334
import java.nio.file.Files;
@@ -237,10 +238,10 @@ protected void save() throws IOException {
237238
}
238239

239240
long now = System.currentTimeMillis();
240-
buf.clear();
241+
((Buffer)buf).clear();
241242
buf.put(MAGIC);
242243
buf.putLong(now);
243-
buf.flip();
244+
((Buffer)buf).flip();
244245
channel.position(0);
245246
channel.write(buf);
246247
//channel.force(true); //handled by SYNC on open option
@@ -261,9 +262,9 @@ private void read() throws IOException {
261262
}
262263

263264
channel.read(buf);
264-
buf.flip();
265-
if (buf.limit() < 16) {
266-
buf.clear();
265+
((Buffer)buf).flip();
266+
if (((Buffer)buf).limit() < 16) {
267+
((Buffer)buf).clear();
267268
throw new IOException(message("Could not read file lock.", null));
268269
}
269270

@@ -274,7 +275,7 @@ private void read() throws IOException {
274275
}
275276

276277
lastHeartbeat = buf.getLong();
277-
buf.clear();
278+
((Buffer)buf).clear();
278279

279280
final DateFormat df = DateFormat.getDateInstance();
280281
message("File lock last access timestamp: " + df.format(getLastHeartbeat()), null);

0 commit comments

Comments
 (0)