1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.tools.jaotc.utils;
25
26 import java.io.ByteArrayOutputStream;
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class NativeOrderOutputStream {
33 private final PatchableByteOutputStream os = new PatchableByteOutputStream();
34 private final byte[] backingArray = new byte[8];
35 private final ByteBuffer buffer;
36 private final List<Patchable> patches = new ArrayList<>();
37 private int size;
38
39 public NativeOrderOutputStream() {
40 buffer = ByteBuffer.wrap(backingArray);
41 buffer.order(ByteOrder.nativeOrder());
42 }
43
44 public static int alignUp(int value, int alignment) {
45 if (Integer.bitCount(alignment) != 1) {
46 throw new IllegalArgumentException("Must be a power of 2");
47 }
48
49 int aligned = (value + (alignment - 1)) & -alignment;
50
51 if (aligned < value || (aligned & (alignment - 1)) != 0) {
52 throw new RuntimeException("Error aligning: " + value + " -> " + aligned);
164 }
165
166 public int value() {
167 return value;
168 }
169
170 @Override
171 public String toString() {
172 final StringBuilder sb = new StringBuilder("PatchableInt{");
173 sb.append("position=").append(super.position()).append(", ");
174 sb.append("patched=").append(patched()).append(", ");
175 sb.append("value=").append(value);
176 sb.append('}');
177 return sb.toString();
178 }
179 }
180
181 private static class PatchableByteOutputStream extends ByteArrayOutputStream {
182
183 public void writeAt(byte[] data, int length, int position) {
184 long end = (long)position + (long)length;
185 if (buf.length < end) {
186 throw new IllegalArgumentException("Array not properly sized");
187 }
188 System.arraycopy(data, 0, buf, position, length);
189 }
190 }
191 }
|
1 /*
2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.tools.jaotc.utils;
25
26 import java.io.ByteArrayOutputStream;
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public final class NativeOrderOutputStream {
33 private final PatchableByteOutputStream os = new PatchableByteOutputStream();
34 private final byte[] backingArray = new byte[8];
35 private final ByteBuffer buffer;
36 private final List<Patchable> patches = new ArrayList<>();
37 private int size;
38
39 public NativeOrderOutputStream() {
40 buffer = ByteBuffer.wrap(backingArray);
41 buffer.order(ByteOrder.nativeOrder());
42 }
43
44 public static int alignUp(int value, int alignment) {
45 if (Integer.bitCount(alignment) != 1) {
46 throw new IllegalArgumentException("Must be a power of 2");
47 }
48
49 int aligned = (value + (alignment - 1)) & -alignment;
50
51 if (aligned < value || (aligned & (alignment - 1)) != 0) {
52 throw new RuntimeException("Error aligning: " + value + " -> " + aligned);
164 }
165
166 public int value() {
167 return value;
168 }
169
170 @Override
171 public String toString() {
172 final StringBuilder sb = new StringBuilder("PatchableInt{");
173 sb.append("position=").append(super.position()).append(", ");
174 sb.append("patched=").append(patched()).append(", ");
175 sb.append("value=").append(value);
176 sb.append('}');
177 return sb.toString();
178 }
179 }
180
181 private static class PatchableByteOutputStream extends ByteArrayOutputStream {
182
183 public void writeAt(byte[] data, int length, int position) {
184 long end = (long) position + (long) length;
185 if (buf.length < end) {
186 throw new IllegalArgumentException("Array not properly sized");
187 }
188 System.arraycopy(data, 0, buf, position, length);
189 }
190 }
191 }
|