Skip to content

Commit a7fe12b

Browse files
committed
Revert "[refactor] Fix issues with ByteBuffer vs Buffer API change in Java 9+"
This can be done now that `<complianceLevel>8</complianceLevel>` has been removed. This reverts commit 3f2965f.
1 parent 3d4c569 commit a7fe12b

File tree

13 files changed

+74
-85
lines changed

13 files changed

+74
-85
lines changed

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

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

4444
import java.io.IOException;
4545
import java.io.PrintStream;
46-
import java.nio.Buffer;
4746
import java.nio.ByteBuffer;
4847
import java.nio.channels.SeekableByteChannel;
4948
import java.nio.file.Files;
@@ -128,19 +127,19 @@ public static void dump(final DigestType digestType, final Path persistentFile,
128127
// write a CSV table header
129128
printStream.println("blobId,referenceCount");
130129

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

133132
final byte[] id = new byte[digestBytesLen];
134133

135134
while (channel.read(buffer) > -1) {
136-
((Buffer)buffer).flip();
135+
buffer.flip();
137136
buffer.get(id);
138137
final BlobId blobId = new BlobId(id);
139138
final int count = buffer.getInt();
140139

141140
printStream.println(blobId + "," + count);
142141

143-
((Buffer)buffer).clear();
142+
buffer.clear();
144143
}
145144
}
146145
}
@@ -157,12 +156,12 @@ public static void dump(final DigestType digestType, final Path persistentFile,
157156
*/
158157
private static void dumpFileHeader(final PrintStream printStream, final ByteBuffer buffer, final Path persistentFile,
159158
final SeekableByteChannel channel) throws IOException {
160-
((Buffer)buffer).clear();
161-
((Buffer)buffer).limit(BLOB_STORE_HEADER_LEN);
159+
buffer.clear();
160+
buffer.limit(BLOB_STORE_HEADER_LEN);
162161

163162
channel.read(buffer);
164163

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

167166
final boolean validMagic =
168167
buffer.get() == BLOB_STORE_MAGIC_NUMBER[0]

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import java.io.IOException;
6161
import java.io.InputStream;
6262
import java.io.OutputStream;
63-
import java.nio.Buffer;
6463
import java.nio.ByteBuffer;
6564
import java.nio.channels.SeekableByteChannel;
6665
import java.nio.file.Files;
@@ -485,7 +484,7 @@ private void closeAfterRecoveryAttempt() {
485484
*/
486485
private void closeBlobStore() {
487486
if (buffer != null) {
488-
((Buffer)buffer).clear();
487+
buffer.clear();
489488
buffer = null;
490489
}
491490

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

566565
validateFileHeader(buffer, persistentFile, channel);
567-
((Buffer)buffer).clear();
566+
buffer.clear();
568567

569568
try (final SeekableByteChannel compactChannel = Files.newByteChannel(compactPersistentFile,
570569
CREATE_NEW, APPEND)) {
571570

572571
writeFileHeader(buffer, compactChannel);
573572

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

576575
while (channel.read(buffer) > -1) {
577576
final byte[] id = new byte[digestType.getDigestLengthBytes()];
578-
((Buffer)buffer).flip();
577+
buffer.flip();
579578
buffer.get(id);
580579
final BlobId blobId = new BlobId(id);
581580
final int count = buffer.getInt();
@@ -587,11 +586,11 @@ private ConcurrentMap<BlobId, BlobReference> compactPersistentReferences(final B
587586

588587
compactReferences.put(blobId, new BlobReference(count, compactChannel.position()));
589588

590-
((Buffer)buffer).flip();
589+
buffer.flip();
591590
compactChannel.write(buffer);
592591
}
593592

594-
((Buffer)buffer).clear();
593+
buffer.clear();
595594
}
596595
}
597596
}
@@ -618,11 +617,11 @@ private ConcurrentMap<BlobId, BlobReference> compactPersistentReferences(final B
618617
private long writeFileHeader(final ByteBuffer buffer, final SeekableByteChannel channel) throws IOException {
619618
final long start = channel.position();
620619

621-
((Buffer)buffer).clear();
620+
buffer.clear();
622621
writeFileHeader(buffer);
623622

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

628627
return channel.position() - start;
@@ -649,12 +648,12 @@ private static void writeFileHeader(final ByteBuffer buffer) {
649648
*/
650649
private void validateFileHeader(final ByteBuffer buffer, final Path file, final SeekableByteChannel channel)
651650
throws IOException {
652-
((Buffer)buffer).clear();
653-
((Buffer)buffer).limit(BLOB_STORE_HEADER_LEN);
651+
buffer.clear();
652+
buffer.limit(BLOB_STORE_HEADER_LEN);
654653

655654
channel.read(buffer);
656655

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

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

12231222
// start immediately after the file header
12241223
channel.position(BLOB_STORE_HEADER_LEN);
12251224

12261225
boolean updatedCount = false;
12271226

12281227
while (channel.read(buffer) > 0) {
1229-
((Buffer)buffer).flip();
1228+
buffer.flip();
12301229
final byte[] id = new byte[digestType.getDigestLengthBytes()];
12311230
buffer.get(id);
12321231
final BlobId readBlobId = new BlobId(id);
12331232

12341233
if (blobId.equals(readBlobId)) {
12351234

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

12411240
channel.write(buffer);
12421241

@@ -1255,11 +1254,11 @@ private void updateBlogRefCount(final BlobId blobId, final int count) throws IOE
12551254
* the next call to compactPersistentReferences
12561255
*/
12571256
if (!updatedCount) {
1258-
((Buffer)buffer).clear();
1257+
buffer.clear();
12591258
buffer.put(blobId.getId());
12601259
buffer.putInt(count);
12611260

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

12641263
channel.write(buffer);
12651264
}
@@ -1618,10 +1617,10 @@ private void writeEntry(final BlobId blobId, final BlobReference blobReference,
16181617

16191618
channel.position(blobReference.persistentOffset);
16201619

1621-
((Buffer)buffer).clear();
1620+
buffer.clear();
16221621
buffer.put(blobId.getId());
16231622
buffer.putInt(newCount);
1624-
((Buffer)buffer).flip();
1623+
buffer.flip();
16251624

16261625
channel.write(buffer);
16271626
}

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

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

2424
import java.io.*;
25-
import java.nio.Buffer;
2625
import java.nio.BufferOverflowException;
2726
import java.nio.ByteBuffer;
2827
import java.nio.channels.FileChannel;
@@ -353,14 +352,14 @@ public synchronized void writeToLog(final Loggable entry) throws JournalExceptio
353352
// 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!
354353
final long pos = channel != null ? channel.position() : 0;
355354

356-
currentLsn = new Lsn(currentJournalFileNumber, pos + ((Buffer)currentBuffer).position() + 1);
355+
currentLsn = new Lsn(currentJournalFileNumber, pos + currentBuffer.position() + 1);
357356
} catch (final IOException e) {
358357
throw new JournalException("Unable to create LSN for: " + entry.dump());
359358
}
360359
entry.setLsn(currentLsn);
361360

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

365364
// write entryHeader
366365
currentBuffer.put(entry.getLogType());
@@ -374,7 +373,7 @@ public synchronized void writeToLog(final Loggable entry) throws JournalExceptio
374373
currentBuffer.putShort((short) (size + LOG_ENTRY_HEADER_LEN));
375374

376375
// write checksum
377-
final long checksum = xxHash64.hash(currentBuffer, currentBufferEntryOffset, ((Buffer)currentBuffer).position() - currentBufferEntryOffset, XXHASH64_SEED);
376+
final long checksum = xxHash64.hash(currentBuffer, currentBufferEntryOffset, currentBuffer.position() - currentBufferEntryOffset, XXHASH64_SEED);
378377
currentBuffer.putLong(checksum);
379378
} catch (final BufferOverflowException e) {
380379
throw new JournalException("Buffer overflow while writing log record: " + entry.dump(), e);
@@ -450,8 +449,8 @@ private void flushBuffer() {
450449
}
451450

452451
try {
453-
if (((Buffer)currentBuffer).position() > 0) {
454-
((Buffer)currentBuffer).flip();
452+
if (currentBuffer.position() > 0) {
453+
currentBuffer.flip();
455454
final int size = currentBuffer.remaining();
456455
while (currentBuffer.hasRemaining()) {
457456
channel.write(currentBuffer);
@@ -462,7 +461,7 @@ private void flushBuffer() {
462461
} catch (final IOException e) {
463462
LOG.warn("Flushing log file failed!", e);
464463
} finally {
465-
((Buffer)currentBuffer).clear();
464+
currentBuffer.clear();
466465
}
467466
}
468467

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

603-
((Buffer)buf).flip();
602+
buf.flip();
604603
channel.write(buf);
605604
}
606605

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

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

3131
import javax.annotation.Nullable;
3232
import java.io.IOException;
33-
import java.nio.Buffer;
3433
import java.nio.ByteBuffer;
3534
import java.nio.channels.SeekableByteChannel;
3635
import java.nio.file.Files;
@@ -83,7 +82,7 @@ private void validateJournalHeader(final Path file, final SeekableByteChannel fc
8382
// read the magic number
8483
final ByteBuffer buf = ByteBuffer.allocateDirect(JOURNAL_HEADER_LEN);
8584
fc.read(buf);
86-
((Buffer)buf).flip();
85+
buf.flip();
8786

8887
// check the magic number
8988
final boolean validMagic =
@@ -146,12 +145,12 @@ Loggable previousEntry() throws LogException {
146145

147146
// go back 8 bytes (checksum length) + 2 bytes (backLink length) and read the backLink (2 bytes) of the last entry
148147
fc.position(fc.position() - LOG_ENTRY_CHECKSUM_LEN - LOG_ENTRY_BACK_LINK_LEN);
149-
((Buffer)header).clear().limit(LOG_ENTRY_BACK_LINK_LEN);
148+
header.clear().limit(LOG_ENTRY_BACK_LINK_LEN);
150149
final int read = fc.read(header);
151150
if (read != LOG_ENTRY_BACK_LINK_LEN) {
152151
throw new LogException("Unable to read journal entry back-link!");
153152
}
154-
((Buffer)header).flip();
153+
header.flip();
155154
final short backLink = header.getShort();
156155

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

198197
// read the entry header
199-
((Buffer)header).clear();
198+
header.clear();
200199
int read = fc.read(header);
201200
if (read <= 0) {
202201
return null;
@@ -205,19 +204,19 @@ Loggable readEntry() throws LogException {
205204
throw new LogException("Incomplete journal entry header found, expected "
206205
+ LOG_ENTRY_HEADER_LEN + " bytes, but found " + read + " bytes");
207206
}
208-
((Buffer)header).flip();
207+
header.flip();
209208

210209
// prepare the checksum for the header
211210
xxHash64.reset();
212211
if (header.hasArray()) {
213212
xxHash64.update(header.array(), 0, LOG_ENTRY_HEADER_LEN);
214213
} else {
215-
final int mark = ((Buffer)header).position();
216-
((Buffer)header).position(0);
214+
final int mark = header.position();
215+
header.position(0);
217216
final byte buf[] = new byte[LOG_ENTRY_HEADER_LEN];
218217
header.get(buf);
219218
xxHash64.update(buf, 0, LOG_ENTRY_HEADER_LEN);
220-
((Buffer)header).position(mark);
219+
header.position(mark);
221220
}
222221

223222
final byte entryType = header.get();
@@ -240,12 +239,12 @@ Loggable readEntry() throws LogException {
240239
// resize the payload buffer
241240
payload = ByteBuffer.allocateDirect(remainingEntryBytes);
242241
}
243-
((Buffer)payload).clear().limit(remainingEntryBytes);
242+
payload.clear().limit(remainingEntryBytes);
244243
read = fc.read(payload);
245244
if (read < remainingEntryBytes) {
246245
throw new LogException("Incomplete log entry found!");
247246
}
248-
((Buffer)payload).flip();
247+
payload.flip();
249248

250249
// read entry data
251250
loggable.read(payload);
@@ -261,12 +260,12 @@ Loggable readEntry() throws LogException {
261260
if (payload.hasArray()) {
262261
xxHash64.update(payload.array(), 0, size + LOG_ENTRY_BACK_LINK_LEN);
263262
} else {
264-
final int mark = ((Buffer)payload).position();
265-
((Buffer)payload).position(0);
263+
final int mark = payload.position();
264+
payload.position(0);
266265
final byte buf[] = new byte[size + LOG_ENTRY_BACK_LINK_LEN];
267266
payload.get(buf);
268267
xxHash64.update(buf, 0, size + LOG_ENTRY_BACK_LINK_LEN);
269-
((Buffer)payload).position(mark);
268+
payload.position(mark);
270269
}
271270

272271
// read the entry checksum

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

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

3030
import java.io.IOException;
31-
import java.nio.Buffer;
3231
import java.nio.ByteBuffer;
3332
import java.nio.channels.SeekableByteChannel;
3433
import java.nio.file.Files;
@@ -238,10 +237,10 @@ protected void save() throws IOException {
238237
}
239238

240239
long now = System.currentTimeMillis();
241-
((Buffer)buf).clear();
240+
buf.clear();
242241
buf.put(MAGIC);
243242
buf.putLong(now);
244-
((Buffer)buf).flip();
243+
buf.flip();
245244
channel.position(0);
246245
channel.write(buf);
247246
//channel.force(true); //handled by SYNC on open option
@@ -262,9 +261,9 @@ private void read() throws IOException {
262261
}
263262

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

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

277276
lastHeartbeat = buf.getLong();
278-
((Buffer)buf).clear();
277+
buf.clear();
279278

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

0 commit comments

Comments
 (0)