Search in sources :

Example 31 with ByteOrder

use of java.nio.ByteOrder in project ffx by mjschnie.

the class CCP4MapWriter method write.

/**
 * write data to file, does not normalize
 *
 * @param data map data to write out
 * @param norm should the data be normalized by mean/sd?
 */
public void write(double[] data, boolean norm) {
    ByteOrder b = ByteOrder.nativeOrder();
    FileOutputStream fos;
    DataOutputStream dos;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    double mean = 0.0;
    double sd = 0.0;
    int n = 0;
    for (int k = 0; k < extz; k++) {
        for (int j = 0; j < exty; j++) {
            for (int i = 0; i < extx; i++) {
                int index = stride * (i + extx * (j + exty * k));
                // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                n++;
                if (data[index] < min) {
                    min = data[index];
                }
                if (data[index] > max) {
                    max = data[index];
                }
                mean += (data[index] - mean) / n;
            }
        }
    }
    n = 0;
    for (int k = 0; k < extz; k++) {
        for (int j = 0; j < exty; j++) {
            for (int i = 0; i < extx; i++) {
                int index = stride * (i + extx * (j + exty * k));
                // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                sd += pow(data[index] - mean, 2.0);
                n++;
            }
        }
    }
    sd = sqrt(sd / n);
    if (norm) {
        for (int k = 0; k < extz; k++) {
            for (int j = 0; j < exty; j++) {
                for (int i = 0; i < extx; i++) {
                    int index = stride * (i + extx * (j + exty * k));
                    data[index] = (data[index] - mean) / sd;
                }
            }
        }
        // recurse
        write(data, false);
    }
    try {
        if (logger.isLoggable(Level.INFO)) {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("\nwriting CCP4 map file: \"%s\"\n", filename));
            sb.append(String.format("map min: %g max: %g mean: %g standard dev.: %g", min, max, mean, sd));
            logger.info(sb.toString());
        }
        fos = new FileOutputStream(filename);
        dos = new DataOutputStream(fos);
        byte[] bytes = new byte[2048];
        int offset = 0;
        int imapdata;
        float fmapdata;
        String mapstr;
        // header
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        bb.order(b).putInt(extx);
        bb.order(b).putInt(exty);
        bb.order(b).putInt(extz);
        // mode (2 = reals, only one we accept)
        bb.order(b).putInt(2);
        bb.order(b).putInt(orix);
        bb.order(b).putInt(oriy);
        bb.order(b).putInt(oriz);
        bb.order(b).putInt(nx);
        bb.order(b).putInt(ny);
        bb.order(b).putInt(nz);
        bb.order(b).putFloat((float) crystal.a);
        bb.order(b).putFloat((float) crystal.b);
        bb.order(b).putFloat((float) crystal.c);
        bb.order(b).putFloat((float) crystal.alpha);
        bb.order(b).putFloat((float) crystal.beta);
        bb.order(b).putFloat((float) crystal.gamma);
        bb.order(b).putInt(1);
        bb.order(b).putInt(2);
        bb.order(b).putInt(3);
        bb.order(b).putFloat((float) min);
        bb.order(b).putFloat((float) max);
        bb.order(b).putFloat((float) mean);
        bb.order(b).putInt(crystal.spaceGroup.number);
        // bb.order(b).putInt(1);
        // symmetry bytes - should set this up at some point
        // imapdata = swap ? ByteSwap.swap(320) : 320;
        bb.order(b).putInt(80);
        bb.order(b).putInt(0);
        for (int i = 0; i < 12; i++) {
            bb.order(b).putFloat(0.0f);
        }
        for (int i = 0; i < 15; i++) {
            bb.order(b).putInt(0);
        }
        dos.write(bytes, offset, 208);
        bb.rewind();
        mapstr = "MAP ";
        dos.writeBytes(mapstr);
        // 0x4441 for LE, 0x1111 for BE
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            imapdata = 0x4441;
        } else {
            imapdata = 0x1111;
        }
        bb.order(b).putInt(imapdata);
        bb.order(b).putFloat((float) sd);
        bb.order(b).putInt(1);
        dos.write(bytes, offset, 12);
        StringBuilder sb = new StringBuilder();
        sb.append("map data from ffx");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dos.writeBytes(sb.toString());
        sb = new StringBuilder();
        while (sb.length() < 80) {
            sb.append(" ");
        }
        for (int i = 0; i < 9; i++) {
            dos.writeBytes(sb.toString());
        }
        sb = new StringBuilder();
        sb.append("x,y,z");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dos.writeBytes(sb.toString());
        bb.rewind();
        for (int k = 0; k < extz; k++) {
            for (int j = 0; j < exty; j++) {
                for (int i = 0; i < extx; i++) {
                    int index = stride * (i + extx * (j + exty * k));
                    // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                    fmapdata = (float) data[index];
                    bb.order(b).putFloat(fmapdata);
                    if (!bb.hasRemaining()) {
                        dos.write(bytes);
                        bb.rewind();
                    }
                }
            }
        }
        if (bb.position() > 0) {
            dos.write(bytes);
            bb.rewind();
        }
        dos.close();
    } catch (Exception e) {
        String message = "Fatal exception evaluating structure factors.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Example 32 with ByteOrder

use of java.nio.ByteOrder in project simple-binary-encoding by real-logic.

the class CSharpGenerator method generateArrayProperty.

private CharSequence generateArrayProperty(final String propertyName, final Token token, final String indent) {
    final String typeName = cSharpTypeName(token.encoding().primitiveType());
    final String typePrefix = toUpperFirstChar(token.encoding().primitiveType().primitiveName());
    final int offset = token.offset();
    final ByteOrder byteOrder = token.encoding().byteOrder();
    final String byteOrderStr = generateByteOrder(byteOrder, token.encoding().primitiveType().size());
    final int fieldLength = token.arrayLength();
    final int typeSize = token.encoding().primitiveType().size();
    final String propName = toUpperFirstChar(propertyName);
    final StringBuilder sb = new StringBuilder();
    sb.append(String.format("\n" + indent + "public const int %sLength = %d;\n", propName, fieldLength));
    sb.append(String.format("\n" + indent + "public %1$s Get%2$s(int index)\n" + indent + "{\n" + indent + INDENT + "if (index < 0 || index >= %3$d)\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "throw new IndexOutOfRangeException(\"index out of range: index=\" + index);\n" + indent + INDENT + "}\n\n" + "%4$s" + indent + INDENT + "return _buffer.%5$sGet%8$s(_offset + %6$d + (index * %7$d));\n" + indent + "}\n", typeName, propName, fieldLength, generateFieldNotPresentCondition(token.version(), token.encoding(), indent), typePrefix, offset, typeSize, byteOrderStr));
    sb.append(String.format("\n" + indent + "public void Set%1$s(int index, %2$s value)\n" + indent + "{\n" + indent + INDENT + "if (index < 0 || index >= %3$d)\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "throw new IndexOutOfRangeException(\"index out of range: index=\" + index);\n" + indent + INDENT + "}\n\n" + indent + INDENT + "_buffer.%4$sPut%7$s(_offset + %5$d + (index * %6$d), value);\n" + indent + "}\n", propName, typeName, fieldLength, typePrefix, offset, typeSize, byteOrderStr));
    if (token.encoding().primitiveType() == PrimitiveType.CHAR) {
        generateCharacterEncodingMethod(sb, propertyName, token.encoding().characterEncoding(), indent);
        sb.append(String.format("\n" + indent + "public int Get%1$s(byte[] dst, int dstOffset)\n" + indent + "{\n" + indent + INDENT + "const int length = %2$d;\n" + indent + INDENT + "if (dstOffset < 0 || dstOffset > (dst.Length - length))\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "throw new IndexOutOfRangeException(" + "\"dstOffset out of range for copy: offset=\" + dstOffset);\n" + indent + INDENT + "}\n\n" + "%3$s" + indent + INDENT + "_buffer.GetBytes(_offset + %4$d, dst, dstOffset, length);\n" + indent + INDENT + "return length;\n" + indent + "}\n", propName, fieldLength, generateArrayFieldNotPresentCondition(token.version(), indent), offset));
        sb.append(String.format("\n" + indent + "public void Set%1$s(byte[] src, int srcOffset)\n" + indent + "{\n" + indent + INDENT + "const int length = %2$d;\n" + indent + INDENT + "if (srcOffset < 0 || srcOffset > src.Length)\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "throw new IndexOutOfRangeException(\"srcOffset out of range for copy: offset=\" + srcOffset);\n" + indent + INDENT + "}\n\n" + indent + INDENT + "if (src.Length > length)\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "throw new ArgumentOutOfRangeException($\"src.Length={src.Length} is too large.\");\n" + indent + INDENT + "}\n\n" + indent + INDENT + "_buffer.SetBytes(_offset + %3$d, src, srcOffset, src.Length - srcOffset);\n" + indent + "}\n", propName, fieldLength, offset));
    }
    return sb;
}
Also used : ByteOrder(java.nio.ByteOrder)

Example 33 with ByteOrder

use of java.nio.ByteOrder in project simple-binary-encoding by real-logic.

the class CSharpGenerator method generateBitSetProperty.

private Object generateBitSetProperty(final String propertyName, final Token token, final String indent) {
    final StringBuilder sb = new StringBuilder();
    final String bitSetName = formatClassName(token.applicableTypeName());
    final int offset = token.offset();
    final String typePrefix = toUpperFirstChar(token.encoding().primitiveType().primitiveName());
    final ByteOrder byteOrder = token.encoding().byteOrder();
    final String byteOrderStr = generateByteOrder(byteOrder, token.encoding().primitiveType().size());
    final String typeName = cSharpTypeName(token.encoding().primitiveType());
    sb.append(String.format("\n" + indent + INDENT + "public %1$s %2$s\n" + indent + INDENT + "{\n" + indent + INDENT + INDENT + "get\n" + indent + INDENT + INDENT + "{\n" + "%3$s" + indent + INDENT + INDENT + INDENT + "return (%4$s)_buffer.%5$sGet%7$s(_offset + %6$d);\n" + indent + INDENT + INDENT + "}\n" + indent + INDENT + INDENT + "set\n" + indent + INDENT + INDENT + "{\n" + indent + INDENT + INDENT + INDENT + "_buffer.%5$sPut%7$s(_offset + %6$d, (%8$s)value);\n" + indent + INDENT + INDENT + "}\n" + indent + INDENT + "}\n", bitSetName, toUpperFirstChar(propertyName), generateTypeFieldNotPresentCondition(token.version(), indent), bitSetName, typePrefix, offset, byteOrderStr, typeName));
    return sb;
}
Also used : ByteOrder(java.nio.ByteOrder)

Example 34 with ByteOrder

use of java.nio.ByteOrder in project jedis by xetorthio.

the class MurmurHash method hash.

/**
 * Hashes the bytes in a buffer from the current position to the limit.
 * @param buf The bytes to hash.
 * @param seed The seed for the hash.
 * @return The 32 bit murmur hash of the bytes in the buffer.
 */
public static int hash(ByteBuffer buf, int seed) {
    // save byte order for later restoration
    ByteOrder byteOrder = buf.order();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    int m = 0x5bd1e995;
    int r = 24;
    int h = seed ^ buf.remaining();
    int k;
    while (buf.remaining() >= 4) {
        k = buf.getInt();
        k *= m;
        k ^= k >>> r;
        k *= m;
        h *= m;
        h ^= k;
    }
    if (buf.remaining() > 0) {
        ByteBuffer finish = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
        // for big-endian version, use this first:
        // finish.position(4-buf.remaining());
        finish.put(buf).rewind();
        h ^= finish.getInt();
        h *= m;
    }
    h ^= h >>> 13;
    h *= m;
    h ^= h >>> 15;
    buf.order(byteOrder);
    return h;
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer)

Example 35 with ByteOrder

use of java.nio.ByteOrder in project databus by linkedin.

the class TestDbusEventV2 method testDbusEventV2SerializationDeserialization.

@Test
public void testDbusEventV2SerializationDeserialization() throws Exception {
    DbusOpcode[] opcodes = new DbusOpcode[] { DbusOpcode.UPSERT, DbusOpcode.DELETE, null };
    DbusEventKey longDek = new DbusEventKey(longKey);
    DbusEventKey strDek = new DbusEventKey(strKey.getBytes(Charset.forName("UTF-8")));
    DbusEventPart schemaKeyPart = new DbusEventPart(DbusEvent.SchemaDigestType.CRC32, keySchemaCrc, keySchemaVersion, ByteBuffer.wrap(schemaKey));
    DbusEventKey schemaDek = new DbusEventKey(schemaKeyPart);
    DbusEventKey[] keys = new DbusEventKey[] { longDek, strDek, schemaDek };
    ByteOrder[] orders = new ByteOrder[] { ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN };
    int[] offsets = new int[] { 0, 5 };
    DbusEventPart md5Metadata = new DbusEventPart(DbusEvent.SchemaDigestType.MD5, payloadSchemaMd5, metadataSchemaVersion, ByteBuffer.wrap(metadataBytes));
    DbusEventPart crcMetadata = new DbusEventPart(DbusEvent.SchemaDigestType.CRC32, crcSignature, metadataSchemaVersion, ByteBuffer.wrap(metadataBytes));
    DbusEventPart[] metadatas = new DbusEventPart[] { null, md5Metadata, crcMetadata };
    for (DbusOpcode opcode : opcodes) {
        for (DbusEventPart metadata : metadatas) {
            DbusEventInfo evInfo = new DbusEventInfo(opcode, seq, partitionId, partitionId, timeStamp, srcId, payloadSchemaMd5, payload, // enabletracing
            false, // auto-commit
            true, DbusEventFactory.DBUS_EVENT_V2, payloadSchemaVersion, // DbusEventPart for metadataBytes
            metadata);
            for (DbusEventKey evKey : keys) {
                for (int offset : offsets) {
                    // buffer.
                    for (ByteOrder byteOrder : orders) {
                        // Replicated event
                        evInfo.setReplicated(true);
                        testSerDeser(evKey, evInfo, offset, byteOrder);
                        // Not a Replicated event
                        evInfo.setReplicated(false);
                        testSerDeser(evKey, evInfo, offset, byteOrder);
                    }
                }
            }
        }
    }
}
Also used : ByteOrder(java.nio.ByteOrder) Test(org.testng.annotations.Test)

Aggregations

ByteOrder (java.nio.ByteOrder)111 ByteBuffer (java.nio.ByteBuffer)39 IOException (java.io.IOException)8 Test (org.junit.Test)7 QuickTest (com.hazelcast.test.annotation.QuickTest)5 DataInputStream (java.io.DataInputStream)5 FileInputStream (java.io.FileInputStream)5 UUID (java.util.UUID)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 HKL (ffx.crystal.HKL)3 DataOutputStream (java.io.DataOutputStream)3 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 ShortBuffer (java.nio.ShortBuffer)3 ArrayList (java.util.ArrayList)3 Element (org.jdom.Element)3 InvalidDumpFormatException (com.ibm.j9ddr.corereaders.InvalidDumpFormatException)2 Point (com.revolsys.geometry.model.Point)2 Crystal (ffx.crystal.Crystal)2 BigInteger (java.math.BigInteger)2