Search in sources :

Example 66 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class ViewTest method testView.

@Test
public void testView() {
    // View takes ownership of constructor argument!
    // Subclasses are responsible for slicing, when needed.
    ByteBuffer buffer = ByteBuffer.allocate(100);
    TestView instance = new TestView(buffer);
    buffer.putInt(12345678);
    int fromBuf = buffer.getInt(0);
    int fromView = instance.getInt(0);
    assertEquals("must assume buffer ownership", fromBuf, fromView);
    int posBuf = buffer.position();
    int posView = instance.buffer.position();
    assertEquals("must assume buffer ownership", posBuf, posView);
}
Also used : ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 67 with ByteBuffer

use of java.nio.ByteBuffer in project bazel by bazelbuild.

the class ZipFiles method unixExternalFileAttributes.

/**
   * Returns the external file attributes of each entry as a mapping from the entry name to the
   * 32-bit value. As long as the attributes are generated by a Unix host, this includes the POSIX
   * file permissions in the upper two bytes. Entries not generated by a Unix host are not included
   * in the result.
   */
public static Map<String, Integer> unixExternalFileAttributes(Path zipFile) throws IOException {
    // Field descriptions in comments were taken from this document:
    // http://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
    ImmutableMap.Builder<String, Integer> attributes = new ImmutableMap.Builder<>();
    try (SeekableByteChannel input = Files.newByteChannel(zipFile)) {
        // The data we care about is toward the end of the file, after the compressed data for each
        // file. We begin by looking for the start of the end of central directory record, which is
        // marked by the signature 0x06054b50
        //
        // This contains the centralDirectoryStartOffset value, which tells us where to seek to find
        // the first central directory entry. Each such entry is marked by the signature 0x02014b50
        // and appear in sequence, one entry for each file in the .zip.
        //
        // The central directory entry contains many values, including the file name, the external
        // file attributes, and the version made by value. If the version made by indicates a Unix
        // host (0x03??), we include the external file attributes in the returned map.
        long offset = input.size() - 4;
        while (offset >= 0) {
            input.position(offset);
            int signature = readBytes(4, input);
            if (signature == 0x06054b50) {
                break;
            } else if (signature == 0x06064b50) {
                throw new IOException("Zip64 format not supported: " + zipFile);
            }
            offset--;
        }
        if (offset < 0) {
            throw new IOException();
        }
        // Read end of central directory structure
        input.position(input.position() + // number of this disk
        2 + // number of the disk with the start of the central directory
        2);
        int entryCount = readBytes(2, input);
        input.position(input.position() + // total number of entries in the central directory
        2);
        input.position(input.position() + // size of the central directory
        4);
        int centralDirectoryStartOffset = readBytes(4, input);
        if (0xffffffff == centralDirectoryStartOffset) {
            throw new IOException("Zip64 format not supported.");
        }
        input.position(centralDirectoryStartOffset);
        int entriesFound = 0;
        // Read each central directory entry
        while ((entriesFound < entryCount) && (readBytes(4, input) == 0x02014b50)) {
            int versionMadeBy = readBytes(2, input);
            input.position(input.position() + // version needed to extract
            2 + // general purpose bit flag
            2 + // compression method
            2 + // last mod file time
            2 + // last mod file date
            2 + // crc-32
            4 + // compressed size
            4 + // uncompressed size
            4);
            int filenameLength = readBytes(2, input);
            int extraFieldLength = readBytes(2, input);
            int fileCommentLength = readBytes(2, input);
            input.position(input.position() + // disk number start
            2 + // internal file attributes
            2);
            int externalFileAttributes = readBytes(4, input);
            input.position(input.position() + // relative offset of local header
            4);
            ByteBuffer filenameBuffer = ByteBuffer.allocate(filenameLength);
            if (filenameLength != input.read(filenameBuffer)) {
                throw new IOException(String.format("Could not read file name (length %d) in central directory record", filenameLength));
            }
            input.position(input.position() + extraFieldLength + fileCommentLength);
            entriesFound++;
            if ((versionMadeBy >> 8) == 3) {
                // Zip made by a Unix host - the external file attributes are POSIX permissions.
                String filename = new String(filenameBuffer.array(), StandardCharsets.UTF_8);
                attributes.put(filename, externalFileAttributes);
            }
        }
        if (entriesFound != entryCount) {
            System.err.printf("WARNING: Expected %d entries in central directory record in '%s', but found %d\n", entryCount, zipFile, entriesFound);
        }
    }
    return attributes.build();
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 68 with ByteBuffer

use of java.nio.ByteBuffer in project stargate-core by tuplejump.

the class RowIndexSupport method addCell.

private void addCell(ByteBuffer rowKey, IndexEntryBuilder builder, Cell cell) {
    CellName cellName = cell.name();
    ColumnIdentifier cql3ColName = cellName.cql3ColumnName(tableMapper.cfMetaData);
    String actualColName = cql3ColName.toString();
    if (logger.isTraceEnabled())
        logger.trace("Got column name {} from CF", actualColName);
    CellName clusteringKey = tableMapper.extractClusteringKey(cell.name());
    ByteBuffer primaryKeyBuff = tableMapper.primaryKey(rowKey, clusteringKey);
    String primaryKey = tableMapper.primaryKeyType.getString(primaryKeyBuff);
    if (builder.isNew(primaryKey)) {
        builder.newPrimaryKey(primaryKey, primaryKeyBuff);
        // new pk found
        if (logger.isTraceEnabled()) {
            logger.trace("New PK found {}", primaryKey);
        }
        //fields for partition key columns need to be added.
        addPartitionKeyFields(rowKey, cell.timestamp(), builder);
        //fields for clustering key columns need to be added.
        addClusteringKeyFields(clusteringKey, cell.timestamp(), builder);
    }
    addCell(cell, cql3ColName, actualColName, builder);
}
Also used : ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) CellName(org.apache.cassandra.db.composites.CellName) ByteBuffer(java.nio.ByteBuffer)

Example 69 with ByteBuffer

use of java.nio.ByteBuffer in project stargate-core by tuplejump.

the class RowIndexSupport method collectionFields.

protected List<Field> collectionFields(CollectionType validator, String colName, Cell column) {
    CellName cellName = column.name();
    List<Field> fields = new ArrayList<>();
    FieldType[] fieldTypesArr = options.collectionFieldTypes.get(colName);
    FieldType docValueType = options.collectionFieldDocValueTypes.get(colName);
    AbstractType keyType = validator.nameComparator();
    FieldCreator keyFieldCreator = CassandraUtils.fromAbstractType(keyType.asCQL3Type()).fieldCreator;
    AbstractType valueType = validator.valueComparator();
    FieldCreator valueFieldCreator = CassandraUtils.fromAbstractType(valueType.asCQL3Type()).fieldCreator;
    ByteBuffer collectionElement = cellName.collectionElement();
    if (validator instanceof MapType) {
        if (fieldTypesArr != null) {
            fields.add(keyFieldCreator.field(colName + "._key", keyType, collectionElement, fieldTypesArr[0]));
            fields.add(valueFieldCreator.field(colName + "._value", valueType, column.value(), fieldTypesArr[1]));
            fields.add(valueFieldCreator.field((colName + "." + keyType.getString(collectionElement)).toLowerCase(), valueType, column.value(), fieldTypesArr[1]));
        }
        if (docValueType != null)
            fields.add(Fields.docValueField((colName + "." + keyType.getString(collectionElement)).toLowerCase(), valueType, column.value(), docValueType));
    } else if (validator instanceof SetType) {
        if (fieldTypesArr != null)
            fields.add(keyFieldCreator.field(colName, keyType, collectionElement, fieldTypesArr[0]));
        if (docValueType != null)
            fields.add(Fields.docValueField(colName, keyType, collectionElement, docValueType));
    } else if (validator instanceof ListType) {
        if (fieldTypesArr != null)
            fields.add(valueFieldCreator.field(colName, valueType, column.value(), fieldTypesArr[0]));
        if (docValueType != null)
            fields.add(Fields.docValueField(colName, valueType, column.value(), docValueType));
    } else
        throw new UnsupportedOperationException("Unsupported collection type " + validator);
    return fields;
}
Also used : CellName(org.apache.cassandra.db.composites.CellName) ByteBuffer(java.nio.ByteBuffer) FieldType(org.apache.lucene.document.FieldType) Field(org.apache.lucene.document.Field)

Example 70 with ByteBuffer

use of java.nio.ByteBuffer in project stargate-core by tuplejump.

the class TableMapper method defaultPartitionKey.

private ByteBuffer defaultPartitionKey() {
    ByteBuffer partitionKey;
    AbstractType keyType = table.metadata.getKeyValidator();
    if (keyType instanceof CompositeType) {
        CompositeType compositeType = ((CompositeType) keyType);
        CompositeType.Builder builder = compositeType.builder();
        compositeType.getComponents();
        for (AbstractType component : compositeType.getComponents()) {
            builder.add(Fields.defaultValue(component));
        }
        partitionKey = builder.build();
    } else {
        partitionKey = Fields.defaultValue(keyType);
    }
    return partitionKey;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8919 Test (org.junit.Test)1965 IOException (java.io.IOException)1022 ArrayList (java.util.ArrayList)450 File (java.io.File)224 FileChannel (java.nio.channels.FileChannel)214 MappedByteBuffer (java.nio.MappedByteBuffer)196 HashMap (java.util.HashMap)177 CharBuffer (java.nio.CharBuffer)153 ByteArrayOutputStream (java.io.ByteArrayOutputStream)146 InetSocketAddress (java.net.InetSocketAddress)143 Random (java.util.Random)127 InputStream (java.io.InputStream)125 Map (java.util.Map)119 FileInputStream (java.io.FileInputStream)116 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)98 IntBuffer (java.nio.IntBuffer)95 SocketChannel (java.nio.channels.SocketChannel)94 FileOutputStream (java.io.FileOutputStream)93