• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / datasketches-java / #306

30 Apr 2024 10:01PM UTC coverage: 97.645% (-0.5%) from 98.139%
#306

push

web-flow
Merge pull request #555 from apache/fix_pom_xml_header

Fix pom xml header

26865 of 27513 relevant lines covered (97.64%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/main/java/org/apache/datasketches/common/ArrayOfUtf16StringsSerDe.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.datasketches.common;
21

22
import static org.apache.datasketches.common.ByteArrayUtil.copyBytes;
23
import static org.apache.datasketches.common.ByteArrayUtil.putIntLE;
24

25
import java.nio.charset.StandardCharsets;
26
import java.util.Objects;
27

28
import org.apache.datasketches.memory.Memory;
29

30
/**
31
 * Methods of serializing and deserializing arrays of String.
32
 * This class serializes strings using internal Java representation as char[], where each char
33
 * is a 16-bit code. The result is larger than one from {@link ArrayOfStringsSerDe}.
34
 * In an extreme case when all strings are in ASCII, the size is doubled. However it takes
35
 * less time to serialize and deserialize by a factor of 1.5 to 2.
36
 *
37
 * @author Alexander Saydakov
38
 */
39
public class ArrayOfUtf16StringsSerDe extends ArrayOfItemsSerDe<String> {
1✔
40

41
  @Override
42
  public byte[] serializeToByteArray(final String item) {
43
    Objects.requireNonNull(item, "Item must not be null");
1✔
44
    final byte[] utf16ByteArr = item.getBytes(StandardCharsets.UTF_16); //includes BOM
1✔
45
    final int numBytes = utf16ByteArr.length;
1✔
46
    final byte[] out = new byte[numBytes + Integer.BYTES];
1✔
47
    copyBytes(utf16ByteArr, 0, out, 4, numBytes);
1✔
48
    putIntLE(out, 0, numBytes);
1✔
49
    return out;
1✔
50
  }
51

52
  @Override
53
  public byte[] serializeToByteArray(final String[] items) {
54
    Objects.requireNonNull(items, "Items must not be null");
1✔
55
    int totalBytes = 0;
1✔
56
    final int numItems = items.length;
1✔
57
    final byte[][] serialized2DArray = new byte[numItems][];
1✔
58
    for (int i = 0; i < numItems; i++) {
1✔
59
      serialized2DArray[i] = items[i].getBytes(StandardCharsets.UTF_16);
1✔
60
      totalBytes += serialized2DArray[i].length + Integer.BYTES;
1✔
61
    }
62
    final byte[] bytesOut = new byte[totalBytes];
1✔
63
    int offset = 0;
1✔
64
    for (int i = 0; i < numItems; i++) {
1✔
65
      final int utf8len = serialized2DArray[i].length;
1✔
66
      putIntLE(bytesOut, offset, utf8len);
1✔
67
      offset += Integer.BYTES;
1✔
68
      copyBytes(serialized2DArray[i], 0, bytesOut, offset, utf8len);
1✔
69
      offset += utf8len;
1✔
70
    }
71
    return bytesOut;
1✔
72
  }
73

74
  @Override
75
  public String[] deserializeFromMemory(final Memory mem, final int numItems) {
76
    return deserializeFromMemory(mem, 0, numItems);
×
77
  }
78

79
  @Override
80
  public String[] deserializeFromMemory(final Memory mem, final long offsetBytes, final int numItems) {
81
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
82
    if (numItems <= 0) { return new String[0]; }
1✔
83
    final String[] array = new String[numItems];
1✔
84
    long offset = offsetBytes;
1✔
85
    for (int i = 0; i < numItems; i++) {
1✔
86
      Util.checkBounds(offset, Integer.BYTES, mem.getCapacity());
1✔
87
      final int strLength = mem.getInt(offset);
1✔
88
      offset += Integer.BYTES;
1✔
89
      final byte[] utf16Bytes = new byte[strLength];
1✔
90
      Util.checkBounds(offset, strLength, mem.getCapacity());
1✔
91
      mem.getByteArray(offset, utf16Bytes, 0, strLength);
1✔
92
      offset += strLength;
1✔
93
      array[i] = new String(utf16Bytes, StandardCharsets.UTF_16);
1✔
94
    }
95
    return array;
1✔
96
  }
97

98
  @Override
99
  public int sizeOf(final String item) {
100
    Objects.requireNonNull(item, "Item must not be null");
1✔
101
    return item.getBytes(StandardCharsets.UTF_16).length + Integer.BYTES;
1✔
102
  }
103

104
  @Override
105
  public int sizeOf(final Memory mem, final long offsetBytes, final int numItems) {
106
    Objects.requireNonNull(mem, "Memory must not be null");
1✔
107
    long offset = offsetBytes;
1✔
108
    final long memCap = mem.getCapacity();
1✔
109
    for (int i = 0; i < numItems; i++) {
1✔
110
      Util.checkBounds(offset, Integer.BYTES, memCap);
1✔
111
      final int itemLenBytes = mem.getInt(offset);
1✔
112
      offset += Integer.BYTES;
1✔
113
      Util.checkBounds(offset, itemLenBytes, memCap);
1✔
114
      offset += itemLenBytes;
1✔
115
    }
116
    return (int)(offset - offsetBytes);
1✔
117
  }
118

119
  @Override
120
  public String toString(final String item) {
121
    if (item == null) { return "null"; }
1✔
122
    return item;
1✔
123
  }
124

125
  @Override
126
  public Class<String> getClassOfT() { return String.class; }
×
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc