-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from jamesmudd/writing
#354 Add Writing Support for file structure
- Loading branch information
Showing
59 changed files
with
1,870 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* This file is part of jHDF. A pure Java library for accessing HDF5 files. | ||
* | ||
* http://jhdf.io | ||
* | ||
* Copyright (c) 2023 James Mudd | ||
* | ||
* MIT License see 'LICENSE' file | ||
*/ | ||
|
||
package io.jhdf; | ||
|
||
import io.jhdf.api.Attribute; | ||
import io.jhdf.api.Group; | ||
import io.jhdf.api.WritableNode; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public abstract class AbstractWritableNode implements WritableNode { | ||
private final Group parent; | ||
private final String name; | ||
|
||
AbstractWritableNode(Group parent, String name) { | ||
this.parent = parent; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public Group getParent() { | ||
return parent; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String getPath() { | ||
if (parent == null) { | ||
return "/" + getName(); | ||
} else { | ||
return parent.getPath() + "/" + getName(); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, Attribute> getAttributes() { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
@Override | ||
public Attribute getAttribute(String name) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public File getFile() { | ||
return parent.getFile(); | ||
} | ||
|
||
@Override | ||
public Path getFileAsPath() { | ||
return parent.getFileAsPath(); | ||
} | ||
|
||
@Override | ||
public HdfFile getHdfFile() { | ||
return parent.getHdfFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* This file is part of jHDF. A pure Java library for accessing HDF5 files. | ||
* | ||
* http://jhdf.io | ||
* | ||
* Copyright (c) 2023 James Mudd | ||
* | ||
* MIT License see 'LICENSE' file | ||
*/ | ||
|
||
package io.jhdf; | ||
|
||
import io.jhdf.checksum.ChecksumUtils; | ||
import io.jhdf.exceptions.HdfException; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
import java.util.Arrays; | ||
import java.util.BitSet; | ||
|
||
import static java.nio.ByteOrder.LITTLE_ENDIAN; | ||
|
||
public class BufferBuilder { | ||
|
||
public static final long UNSIGNED_BYTE_MAX = Byte.MAX_VALUE * 2L; | ||
public static final long UNSIGNED_SHORT_MAX = Short.MAX_VALUE * 2L; | ||
private static final ByteOrder BYTE_ORDER = LITTLE_ENDIAN; | ||
|
||
private final ByteArrayOutputStream byteArrayOutputStream; | ||
private final DataOutputStream dataOutputStream; // Note always big endian | ||
|
||
public BufferBuilder() { | ||
this.byteArrayOutputStream = new ByteArrayOutputStream(); | ||
this.dataOutputStream = new DataOutputStream(byteArrayOutputStream); | ||
} | ||
|
||
public BufferBuilder writeByte(int i) { | ||
try { | ||
dataOutputStream.writeByte(i); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder writeBytes(byte[] bytes) { | ||
try { | ||
dataOutputStream.write(bytes); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder writeShort(int i) { | ||
try { | ||
short s = (short) i; | ||
if(BYTE_ORDER == LITTLE_ENDIAN) { | ||
s = Short.reverseBytes(s); | ||
} | ||
dataOutputStream.writeShort(s); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder writeInt(int i) { | ||
try { | ||
if(BYTE_ORDER == LITTLE_ENDIAN) { | ||
i = Integer.reverseBytes(i); | ||
} | ||
dataOutputStream.writeInt(i); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder writeLong(long l) { | ||
try { | ||
if(BYTE_ORDER == LITTLE_ENDIAN) { | ||
l = Long.reverseBytes(l); | ||
} | ||
dataOutputStream.writeLong(l); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public ByteBuffer build() { | ||
try { | ||
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArrayOutputStream.toByteArray()); | ||
byteBuffer.order(BYTE_ORDER); | ||
dataOutputStream.close(); | ||
byteArrayOutputStream.close(); | ||
return byteBuffer; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder writeBitSet(BitSet bitSet, int length) { | ||
if(bitSet.toByteArray().length > length) { | ||
throw new IllegalArgumentException("BitSet is longer than length provided"); | ||
} | ||
try { | ||
final byte[] bytes = Arrays.copyOf(bitSet.toByteArray(), length); // Ensure empty Bitset are not shortened | ||
dataOutputStream.write(bytes); | ||
return this; | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
} | ||
|
||
public BufferBuilder appendChecksum() { | ||
writeInt(ChecksumUtils.checksum(byteArrayOutputStream.toByteArray())); | ||
return this; | ||
} | ||
|
||
public BufferBuilder writeBuffer(ByteBuffer byteBuffer) { | ||
try { | ||
dataOutputStream.write(byteBuffer.array()); | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
return this; | ||
} | ||
|
||
public BufferBuilder writeShortestRepresentation(int i) { | ||
try { | ||
if (i <= UNSIGNED_BYTE_MAX) { | ||
dataOutputStream.writeByte(i); | ||
} else if(i <= UNSIGNED_SHORT_MAX) { | ||
dataOutputStream.writeShort(i); | ||
} else { | ||
dataOutputStream.write(i); | ||
} | ||
} catch (IOException e) { | ||
throw new BufferBuilderException(e); | ||
} | ||
return this; | ||
} | ||
|
||
public static final class BufferBuilderException extends HdfException { | ||
private BufferBuilderException(String message, Throwable throwable) { | ||
super(message, throwable); | ||
} | ||
|
||
private BufferBuilderException(Throwable throwable) { | ||
this("Error in BufferBuilder", throwable); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BufferBuilder{" + | ||
"bytesWriten=" + dataOutputStream.size() + | ||
"}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.