append Dh to unclosableStreams and refactor

This commit is contained in:
James Seibel
2023-03-09 07:14:48 -06:00
parent f691f016bc
commit ce472729d6
9 changed files with 26 additions and 25 deletions
@@ -10,7 +10,7 @@ import com.seibel.lod.core.pos.DhLodPos;
import com.seibel.lod.core.file.fullDatafile.FullDataMetaFile;
import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.BitShiftUtil;
import com.seibel.lod.core.util.objects.UnclosableInputStream;
import com.seibel.lod.core.util.objects.DhUnclosableInputStream;
import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.util.LodUtil;
import org.apache.logging.log4j.Logger;
@@ -245,7 +245,7 @@ public class FullDataSource extends FullArrayView implements IFullDataSource
throw new IOException("invalid data content end guard");
}
FullDataPointIdMap mapping = FullDataPointIdMap.deserialize(new UnclosableInputStream(dos));
FullDataPointIdMap mapping = FullDataPointIdMap.deserialize(new DhUnclosableInputStream(dos));
end = dos.readInt();
if (end != 0xFFFFFFFF)
{
@@ -11,7 +11,7 @@ import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.pos.DhLodPos;
import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.LodUtil;
import com.seibel.lod.core.util.objects.UnclosableInputStream;
import com.seibel.lod.core.util.objects.DhUnclosableInputStream;
import org.apache.logging.log4j.Logger;
import java.io.*;
@@ -180,7 +180,7 @@ public class SingleChunkFullDataSource extends FullArrayView implements IIncompl
if (end != 0xFFFFFFFF)
throw new IOException("invalid data content end guard");
FullDataPointIdMap mapping = FullDataPointIdMap.deserialize(new UnclosableInputStream(dos));
FullDataPointIdMap mapping = FullDataPointIdMap.deserialize(new DhUnclosableInputStream(dos));
end = dos.readInt();
if (end != 0xFFFFFFFF)
throw new IOException("invalid id mapping end guard");
@@ -202,6 +202,11 @@ public class ColumnRenderSource
// data update and output //
//========================//
public void saveRender(IDhClientLevel level, RenderMetaDataFile file, OutputStream dataStream) throws IOException
{
DataOutputStream dos = new DataOutputStream(dataStream); // DO NOT CLOSE
this.writeData(dos);
}
void writeData(DataOutputStream outputStream) throws IOException
{
outputStream.writeByte(this.getDataDetail());
@@ -429,12 +434,6 @@ public class ColumnRenderSource
return false;
}
public void saveRender(IDhClientLevel level, RenderMetaDataFile file, OutputStream dataStream) throws IOException
{
DataOutputStream dos = new DataOutputStream(dataStream); // DO NOT CLOSE
this.writeData(dos);
}
public byte getRenderVersion() { return DATA_FORMAT_VERSION; }
/**
@@ -427,7 +427,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider
IFullDataSource sourceLocal = source;
boolean changed = updater.apply(sourceLocal);
// if (changed)
// if (changed)
// {
// metaData.dataVersion.incrementAndGet();
// }
@@ -439,6 +439,7 @@ public class FullDataFileHandler implements IFullDataSourceProvider
changed |= newSource != sourceLocal;
sourceLocal = newSource;
}
if (changed)
{
onUpdated.accept(sourceLocal);
@@ -420,7 +420,7 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile
private FileInputStream getDataContent() throws IOException
{
FileInputStream fin = new FileInputStream(this.file);
int toSkip = METADATA_SIZE;
int toSkip = METADATA_SIZE_IN_BYTES;
while (toSkip > 0)
{
long skipped = fin.skip(toSkip);
@@ -3,7 +3,6 @@ package com.seibel.lod.core.file.metaData;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.file.FileAlreadyExistsException;
@@ -15,7 +14,7 @@ import java.util.zip.CheckedOutputStream;
import com.seibel.lod.core.util.FileUtil;
import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.objects.UnclosableOutputStream;
import com.seibel.lod.core.util.objects.DhUnclosableOutputStream;
import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.util.LodUtil;
import org.apache.logging.log4j.Logger;
@@ -51,7 +50,8 @@ public abstract class AbstractMetaDataContainerFile
{
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
public static final int METADATA_SIZE = 64;
public static final int METADATA_SIZE_IN_BYTES = 64;
// public static final int BUFFER_SIZE = 8192;
public static final int METADATA_RESERVED_SIZE = 24;
/** equivalent to "DHv0" */
public static final int METADATA_IDENTITY_BYTES = 0x44_48_76_30;
@@ -119,7 +119,7 @@ public abstract class AbstractMetaDataContainerFile
{
try (FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ))
{
ByteBuffer byteBuffer = ByteBuffer.allocate(METADATA_SIZE);
ByteBuffer byteBuffer = ByteBuffer.allocate(METADATA_SIZE_IN_BYTES);
channel.read(byteBuffer, 0);
channel.close();
byteBuffer.flip();
@@ -203,20 +203,21 @@ public abstract class AbstractMetaDataContainerFile
try (FileChannel file = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING))
{
file.position(METADATA_SIZE);
file.position(METADATA_SIZE_IN_BYTES);
int checksum;
try (OutputStream channelOut = new UnclosableOutputStream(Channels.newOutputStream(file)); // Prevent closing the channel
try (OutputStream channelOut = new DhUnclosableOutputStream(Channels.newOutputStream(file)); // Prevent closing the channel
BufferedOutputStream bufferedOut = new BufferedOutputStream(channelOut); // TODO: Is default buffer size ok? Do we even need to buffer?
CheckedOutputStream checkedOut = new CheckedOutputStream(bufferedOut, new Adler32()))
{
{
// TODO: Is Adler32 ok?
dataWriterFunc.writeBufferToFile(checkedOut);
checksum = (int) checkedOut.getChecksum().getValue();
}
file.position(0);
// Write metadata
ByteBuffer buff = ByteBuffer.allocate(METADATA_SIZE);
ByteBuffer buff = ByteBuffer.allocate(METADATA_SIZE_IN_BYTES);
buff.putInt(METADATA_IDENTITY_BYTES);
buff.putInt(this.pos.sectionX);
buff.putInt(Integer.MIN_VALUE); // Unused
@@ -231,7 +231,7 @@ public class RenderMetaDataFile extends AbstractMetaDataContainerFile
private FileInputStream getDataContent() throws IOException
{
FileInputStream fin = new FileInputStream(this.file);
int toSkip = METADATA_SIZE;
int toSkip = METADATA_SIZE_IN_BYTES;
while (toSkip > 0)
{
long skipped = fin.skip(toSkip);
@@ -4,8 +4,8 @@ import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class UnclosableInputStream extends FilterInputStream {
public UnclosableInputStream(InputStream it) {
public class DhUnclosableInputStream extends FilterInputStream {
public DhUnclosableInputStream(InputStream it) {
super(it);
}
@@ -4,8 +4,8 @@ import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class UnclosableOutputStream extends FilterOutputStream {
public UnclosableOutputStream(OutputStream it) {
public class DhUnclosableOutputStream extends FilterOutputStream {
public DhUnclosableOutputStream(OutputStream it) {
super(it);
}
@Override