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

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

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

8.7
/SpiNNaker-utils/src/main/java/uk/ac/manchester/spinnaker/utils/ByteBufferUtils.java
1
/*
2
 * Copyright (c) 2022 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.utils;
17

18
import static java.lang.Math.min;
19
import static java.nio.ByteOrder.LITTLE_ENDIAN;
20

21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.nio.ByteBuffer;
24
import java.util.Iterator;
25

26
/** Utility methods for {@link ByteBuffer}s. */
27
public abstract class ByteBufferUtils {
28
        private ByteBufferUtils() {
29
        }
30

31
        private static final int WORD_SIZE = 4;
32

33
        /**
34
         * Allocate a new little-endian byte buffer.
35
         *
36
         * @param capacity
37
         *            The capacity of the buffer.
38
         * @return The buffer.
39
         */
40
        public static ByteBuffer alloc(int capacity) {
41
                return ByteBuffer.allocate(capacity).order(LITTLE_ENDIAN);
1✔
42
        }
43

44
        /**
45
         * Convert a word to a buffer that could form part of a message understood
46
         * by SpiNNaker.
47
         *
48
         * @param value
49
         *            The value to put in the buffer as a single 32-bit word.
50
         * @return The buffer, flipped. The buffer is writable and has a backing
51
         *         array.
52
         */
53
        public static ByteBuffer wordAsBuffer(int value) {
54
                var b = alloc(WORD_SIZE);
×
55
                b.putInt(value).flip();
×
56
                return b;
×
57
        }
58

59
        /**
60
         * Make a slice of a byte buffer if it exceeds a given size.
61
         *
62
         * @param src
63
         *            The originating buffer.
64
         * @param maxSize
65
         *            The maximum size of the resulting buffer.
66
         * @return The original buffer or a little-endian slice. This will be
67
         *         read-only if and only if the original buffer is read-only.
68
         */
69
        public static ByteBuffer limitSlice(ByteBuffer src, int maxSize) {
70
                if (src.remaining() <= maxSize) {
×
71
                        return src;
×
72
                }
73
                return src.slice().order(LITTLE_ENDIAN).limit(maxSize);
×
74
        }
75

76
        /**
77
         * Slice up a buffer buffer into a sequence of (little-endian) byte buffers
78
         * with a maximum size.
79
         *
80
         * @param src
81
         *            The buffer to slice up.
82
         * @param chunkSize
83
         *            Max size of each chunk. Must be positive.
84
         * @return An iterable of little-endian chunks. Only the final chunk will be
85
         *         smaller than the requested chunk size.
86
         */
87
        public static MappableIterable<ByteBuffer> sliceUp(ByteBuffer src,
88
                        int chunkSize) {
89
                return () -> new Iterator<>() {
×
90
                        final ByteBuffer b = src.duplicate();
×
91

92
                        @Override
93
                        public boolean hasNext() {
94
                                return b.hasRemaining();
×
95
                        }
96

97
                        @Override
98
                        public ByteBuffer next() {
99
                                var s = b.slice();
×
100
                                s.limit(min(chunkSize, s.limit()));
×
101
                                b.position(b.position() + s.limit());
×
102
                                return s.order(LITTLE_ENDIAN);
×
103
                        }
104
                };
105
        }
106

107
        /**
108
         * Read a chunk of an input stream into a byte buffer.
109
         *
110
         * @param data
111
         *            Where to read from.
112
         * @param workingBuffer
113
         *            The buffer to use. The number of bytes remaining in the buffer
114
         *            is the maximum number of bytes to read <em>unless</em> the
115
         *            maximum bytes remaining is smaller.
116
         * @param maxRemaining
117
         *            The maximum number of bytes remaining in the input stream;
118
         *            bytes after that point won't be read even if they exist.
119
         * @return A view on the buffer with the data in it (endianness undefined),
120
         *         or {@code null} if EOF or the limit is reached.
121
         * @throws IOException
122
         *             If reading fails.
123
         */
124
        public static ByteBuffer read(InputStream data, ByteBuffer workingBuffer,
125
                        int maxRemaining) throws IOException {
126
                var tmp = workingBuffer.duplicate();
×
127
                int size = min(tmp.remaining(), maxRemaining);
×
128
                if (size <= 0) {
×
129
                        return null;
×
130
                }
131
                size = data.read(tmp.array(), tmp.arrayOffset(), size);
×
132
                if (size <= 0) {
×
133
                        return null;
×
134
                }
135
                return tmp.limit(size);
×
136
        }
137

138
        /**
139
         * Convert the remaining bytes in a buffer into a read-only buffer.
140
         *
141
         * @param buffer
142
         *            message buffer to convert
143
         * @return The read-only view.
144
         */
145
        public static ByteBuffer readOnly(ByteBuffer buffer) {
146
                return buffer.asReadOnlyBuffer().order(LITTLE_ENDIAN);
1✔
147
        }
148
}
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

© 2026 Coveralls, Inc