1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.suggest.popular.impl.chronicle; 24 25 import net.openhft.chronicle.bytes.Bytes; 26 import net.openhft.chronicle.core.io.IORuntimeException; 27 import net.openhft.chronicle.core.util.ReadResolvable; 28 import net.openhft.chronicle.hash.serialization.SizedReader; 29 import net.openhft.chronicle.wire.Marshallable; 30 import net.openhft.chronicle.wire.WireIn; 31 import net.openhft.chronicle.wire.WireOut; 32 import org.apache.lucene.util.BytesRef; 33 import org.jetbrains.annotations.NotNull; 34 import org.jetbrains.annotations.Nullable; 35 36 /** 37 * {@link BytesRef} data serializer for {@link net.openhft.chronicle.map.ChronicleMap}. 38 * Modified from https://github.com/OpenHFT/Chronicle-Map/blob/master/docs/CM_Tutorial_DataAccess.adoc 39 */ 40 public class BytesRefSizedReader implements SizedReader<BytesRef>, Marshallable, ReadResolvable<BytesRefSizedReader> { 41 42 public static final BytesRefSizedReader INSTANCE = new BytesRefSizedReader(); 43 BytesRefSizedReader()44 private BytesRefSizedReader() { 45 } 46 47 @NotNull 48 @Override 49 @SuppressWarnings("rawtypes") read(Bytes in, long size, @Nullable BytesRef using)50 public BytesRef read(Bytes in, long size, @Nullable BytesRef using) { 51 if (size < 0L || size > (long) Integer.MAX_VALUE) { 52 throw new IORuntimeException("byte[] size should be non-negative int, " + 53 size + " given. Memory corruption?"); 54 } 55 int arrayLength = (int) size; 56 if (using == null) { 57 using = new BytesRef(new byte[arrayLength]); 58 } else if (using.bytes.length < arrayLength) { 59 using.bytes = new byte[arrayLength]; 60 } 61 in.read(using.bytes, 0, arrayLength); 62 using.offset = 0; 63 using.length = arrayLength; 64 return using; 65 } 66 67 @Override writeMarshallable(@otNull WireOut wireOut)68 public void writeMarshallable(@NotNull WireOut wireOut) { 69 // no fields to write 70 } 71 72 @Override readMarshallable(@otNull WireIn wireIn)73 public void readMarshallable(@NotNull WireIn wireIn) { 74 // no fields to read 75 } 76 77 @NotNull 78 @Override readResolve()79 public BytesRefSizedReader readResolve() { 80 return INSTANCE; 81 } 82 83 } 84