• 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

98.28
/src/main/java/org/apache/datasketches/common/ArrayOfStringsSerDe.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 in UTF-8 format, which is more compact compared to
33
 * {@link ArrayOfUtf16StringsSerDe}. In an extreme case when all strings are in ASCII,
34
 * this method is 2 times more compact, but it takes more time to encode and decode
35
 * by a factor of 1.5 to 2.
36
 *
37
 * <p>The serialization
38
 *
39
 * @author Alexander Saydakov
40
 */
41
public class ArrayOfStringsSerDe extends ArrayOfItemsSerDe<String> {
1✔
42

43
  @Override
44
  public byte[] serializeToByteArray(final String item) {
45
    Objects.requireNonNull(item, "Item must not be null");
1✔
46
    if (item.isEmpty()) { return new byte[] { 0, 0, 0, 0 }; }
1✔
47
    final byte[] utf8ByteArr = item.getBytes(StandardCharsets.UTF_8);
1✔
48
    final int numBytes = utf8ByteArr.length;
1✔
49
    final byte[] out = new byte[numBytes + Integer.BYTES];
1✔
50
    copyBytes(utf8ByteArr, 0, out, 4, numBytes);
1✔
51
    putIntLE(out, 0, numBytes);
1✔
52
    return out;
1✔
53
  }
54

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

78
  @Override
79
  public String[] deserializeFromMemory(final Memory mem, final int numItems) {
80
    return deserializeFromMemory(mem, 0, numItems);
×
81
  }
82

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

102
  @Override
103
  public int sizeOf(final String item) {
104
    Objects.requireNonNull(item, "Item must not be null");
1✔
105
    if (item.isEmpty()) { return Integer.BYTES; }
1✔
106
    return item.getBytes(StandardCharsets.UTF_8).length + Integer.BYTES;
1✔
107
  }
108

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

125
  @Override
126
  public String toString(final String item) {
127
    if (item == null) { return "null"; }
1✔
128
    return item;
1✔
129
  }
130

131
  @Override
132
  public Class<String> getClassOfT() { return String.class; }
1✔
133
}
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