From 4ec2bdf4402bf836cf916f15d8a011689bb1dab8 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Mon, 7 Feb 2011 10:29:44 +0000 Subject: [PATCH] add: getByteOrder() and setByteOrder() to IRandomAccessFile SVN: 19787 --- .../ByteBufferRandomAccessFile.java | 13 ++- .../common/filesystem/IRandomAccessFile.java | 20 +++- .../filesystem/RandomAccessFileImpl.java | 94 ++++++++++++++----- 3 files changed, 101 insertions(+), 26 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/ByteBufferRandomAccessFile.java b/common/source/java/ch/systemsx/cisd/common/filesystem/ByteBufferRandomAccessFile.java index 1787dfabfb3..6039ae26231 100644 --- a/common/source/java/ch/systemsx/cisd/common/filesystem/ByteBufferRandomAccessFile.java +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/ByteBufferRandomAccessFile.java @@ -18,6 +18,7 @@ package ch.systemsx.cisd.common.filesystem; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; @@ -40,6 +41,16 @@ public class ByteBufferRandomAccessFile implements IRandomAccessFile this.buf = buf; } + public ByteOrder getByteOrder() + { + return buf.order(); + } + + public void setByteOrder(ByteOrder byteOrder) + { + buf.order(byteOrder); + } + public void readFully(byte[] b) throws IOExceptionUnchecked { readFully(b, 0, b.length); @@ -74,7 +85,7 @@ public class ByteBufferRandomAccessFile implements IRandomAccessFile public int read() throws IOExceptionUnchecked { - return buf.getInt(); + return buf.get() & 0xff; } public int read(byte[] b) throws IOExceptionUnchecked diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/IRandomAccessFile.java b/common/source/java/ch/systemsx/cisd/common/filesystem/IRandomAccessFile.java index 55172e6a790..166a9ee6a9f 100644 --- a/common/source/java/ch/systemsx/cisd/common/filesystem/IRandomAccessFile.java +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/IRandomAccessFile.java @@ -19,6 +19,7 @@ package ch.systemsx.cisd.common.filesystem; import java.io.Closeable; import java.io.DataInput; import java.io.DataOutput; +import java.nio.ByteOrder; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; @@ -30,6 +31,17 @@ import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; public interface IRandomAccessFile extends DataInput, DataOutput, Closeable, IInputStream, IOutputStream { + /** + * Gets the byte-order (endiness) of the random access file. Default is network-byte order + * (big-endian). + */ + public ByteOrder getByteOrder(); + + /** + * Sets the byte-order (endiness) of the random access file. + */ + public void setByteOrder(ByteOrder byteOrder); + // // RandomAccessFile // @@ -51,8 +63,8 @@ public interface IRandomAccessFile extends DataInput, DataOutput, Closeable, IIn * * @param pos the offset position, measured in bytes from the beginning of the file, at which to * set the file pointer. - * @exception IOExceptionUnchecked if <code>pos</code> is less than <code>0</code> or if an I/O error - * occurs. + * @exception IOExceptionUnchecked if <code>pos</code> is less than <code>0</code> or if an I/O + * error occurs. */ public void seek(long pos) throws IOExceptionUnchecked; @@ -81,7 +93,7 @@ public interface IRandomAccessFile extends DataInput, DataOutput, Closeable, IIn * @exception IOExceptionUnchecked If an I/O error occurs */ public void setLength(long newLength) throws IOExceptionUnchecked; - + // // DataInput // @@ -160,7 +172,7 @@ public interface IRandomAccessFile extends DataInput, DataOutput, Closeable, IIn * @see DataInput#readUTF() */ public String readUTF() throws IOExceptionUnchecked; - + // // DataOutput // diff --git a/common/source/java/ch/systemsx/cisd/common/filesystem/RandomAccessFileImpl.java b/common/source/java/ch/systemsx/cisd/common/filesystem/RandomAccessFileImpl.java index a932284974d..95ac97129c6 100644 --- a/common/source/java/ch/systemsx/cisd/common/filesystem/RandomAccessFileImpl.java +++ b/common/source/java/ch/systemsx/cisd/common/filesystem/RandomAccessFileImpl.java @@ -19,6 +19,7 @@ package ch.systemsx.cisd.common.filesystem; import java.io.FileDescriptor; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.ByteOrder; import java.nio.channels.FileChannel; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; @@ -34,6 +35,40 @@ public class RandomAccessFileImpl implements IRandomAccessFile private final RandomAccessFile raf; + private ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; + + private boolean changeByteOrder = ByteOrder.nativeOrder().equals(byteOrder); + + private static short changeByteOrder(short x) + { + return (short) ((x << 8) | ((x >> 8) & 0xff)); + } + + private static char changeByteOrder(char x) + { + return (char) ((x << 8) | ((x >> 8) & 0xff)); + } + + private static int changeByteOrder(int x) + { + return ((changeByteOrder((short) x) << 16) | (changeByteOrder((short) (x >> 16)) & 0xffff)); + } + + private static float changeByteOrder(float x) + { + return Float.intBitsToFloat(changeByteOrder(Float.floatToIntBits(x))); + } + + private static long changeByteOrder(long x) + { + return (((long) changeByteOrder((int) (x)) << 32) | (changeByteOrder((int) (x >> 32)) & 0xffffffffL)); + } + + private static double changeByteOrder(double x) + { + return Double.longBitsToDouble(changeByteOrder(Double.doubleToLongBits(x))); + } + public RandomAccessFileImpl(RandomAccessFile raf) { this.raf = raf; @@ -49,6 +84,17 @@ public class RandomAccessFileImpl implements IRandomAccessFile return raf.getChannel(); } + public ByteOrder getByteOrder() + { + return byteOrder; + } + + public void setByteOrder(ByteOrder byteOrder) + { + this.byteOrder = byteOrder; + this.changeByteOrder = ByteOrder.nativeOrder().equals(byteOrder); + } + public int read() throws IOExceptionUnchecked { try @@ -240,7 +286,8 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - return raf.readShort(); + final short s = raf.readShort(); + return changeByteOrder ? changeByteOrder(s) : s; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -249,20 +296,15 @@ public class RandomAccessFileImpl implements IRandomAccessFile public final int readUnsignedShort() throws IOExceptionUnchecked { - try - { - return raf.readUnsignedShort(); - } catch (IOException ex) - { - throw CheckedExceptionTunnel.wrapIfNecessary(ex); - } + return readShort() & 0xffff; } public final char readChar() throws IOExceptionUnchecked { try { - return raf.readChar(); + final char c = raf.readChar(); + return changeByteOrder ? changeByteOrder(c) : c; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -273,7 +315,8 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - return raf.readInt(); + final int i = raf.readInt(); + return changeByteOrder ? changeByteOrder(i) : i; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -284,7 +327,8 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - return raf.readLong(); + final long l = raf.readLong(); + return changeByteOrder ? changeByteOrder(l) : l; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -295,7 +339,8 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - return raf.readFloat(); + final float f = raf.readFloat(); + return changeByteOrder ? changeByteOrder(f) : f; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -306,7 +351,8 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - return raf.readDouble(); + final double d = raf.readDouble(); + return changeByteOrder ? changeByteOrder(d) : d; } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -361,7 +407,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeShort(v); + raf.writeShort(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -372,7 +418,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeChar(v); + raf.writeChar(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -383,7 +429,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeInt(v); + raf.writeInt(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -394,7 +440,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeLong(v); + raf.writeLong(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -405,7 +451,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeFloat(v); + raf.writeFloat(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -416,7 +462,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile { try { - raf.writeDouble(v); + raf.writeDouble(changeByteOrder ? changeByteOrder(v) : v); } catch (IOException ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); @@ -509,7 +555,7 @@ public class RandomAccessFileImpl implements IRandomAccessFile } private long markedPosition = -1; - + public void mark(int readlimit) { try @@ -548,7 +594,13 @@ public class RandomAccessFileImpl implements IRandomAccessFile public void synchronize() throws IOExceptionUnchecked { - // NOOP + try + { + raf.getFD().sync(); + } catch (IOException ex) + { + throw CheckedExceptionTunnel.wrapIfNecessary(ex); + } } } -- GitLab