Is there a convenient class library in Java for manipulating byte[], such as writing other types of data by subscript? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 days ago.
Improve this question
I'm rewriting a piece of Go code in Java:
//instruction is a byte[], o is an int.
//Start writing an int type data from the offset subscript of the instruction
binary.BigEndian.PutUint16(instruction[offset:], uint16(o))
I need to achieve a similar effect to a byte[] in Java, but I can't find a class library in Java that can easily achieve this kind of operation.
When I use copilot, it prompts me to use bit operations to do the job, but it's too tedious and unreadable, I don't want to do that, so I need a class library.

One way would be to wrap the byte array in a ByteBuffer and use its various putXXX methods.
e.g.
// "wrap" makes a big endian byte buffer by default
var buffer = ByteBuffer.wrap(yourByteArray);
// puts 300 as a 16 bit signed integer (short) at index 1
buffer.putShort(1, (short)300);
There are also put (for byte), putChar, putDouble, putFloat, putLong, putInt.

You could use MethodHandles::byteArrayViewVarHandle:
var handle = MethodHandles.byteArrayViewVarHandle(short[].class, ByteOrder.nativeOrder());
byte[] arr = new byte[2];
handle.set(arr, 0, (short) 42);
// arr ==> byte[2] { 42, 0 }

If you access the data sequentially, the time-honored DataOutputStream would fit, too:
var baos = new ByteArrayOutputStream();
try (var out = new DataOutputStream(baos)) {
out.writeShort(0xfffe);
}
var bytes = baos.toByteArray();
System.out.println(Arrays.toString(bytes));
try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
var us = in.readUnsignedShort();
System.out.println(us);
}
(if you don't need the byte[], you can of course stream directly to/from a file, socket, ...)

Related

Can 'split' be used? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I get this error "cannot resolve symbol method ‘split’ (java lang string)", when I try using the code below.
What could be causing the "split" error?
What is required to use "split" do I need to import android?
public void run() {
byte[] buffer = new byte[2048];
int mybytes;
String fields[];
while (true) {
try {
mybytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, mybytes);
System.out.print("|mybytes|:\t" + mybytes);
fields = mybytes.split(" ");
heat = Integer.parseInt(fields[1]);
speed = Integer.parseInt(fields[3]);
You need to Declare Your mybytes as String not as a int change it
Use this
String mybytes;
Instead of this
int mybytes;
Okay, first off I'll address your question:
You're trying to perform a split on an int, not a stream, change the line
fields = mybytes.split(" ");
to
fields = readMessage.split(" ");
Regarding the structure of the questions itself:
Next time you're going to ask a question refer to How to ask.
Please provide the log-cat (or just stack-trace) for the error.
Please explain what you tried to do.
Please provide the entire code related to your problem, and not just a couple of lines with a bunch of blank lines inside of them.
Again, next time please refer to the How to ask because if you post a question in the same format, you might very well be blocked from asking questions again.
I think you intend to split buffer, not mbytes
You are trying to split an int. Instead, use a String, like :
String mybytes;
mybytes = String.valueOf(mmInStream.read(buffer));
fields = mybytes.split(" ");
Or you can use
fields = readMessage.split(" ");

How to write a String to fixed-size text files in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a very long string and I want to write it to several text files of fixed size. For example, I want to set the size to be 1MB per file, and label each file as "text01.txt", "text02.txt"...
How can I achieve this in the simplest way?
Keep track of the number of bytes you're writing, and when it reaches a specified point, close the existing file and continue in a new one. There's no need to analyze the size of the file, since you know exactly what's going into it.
Something like this:
long fileSizeByteLimit = 5000000;
long bytesOutput = 0;
while(THEREAREMORELINESTOOUTPUT) {
//Open a new file
while(bytesOutput <= fileSizeByteLimit) {
writer.append(lineOfOutput);
bytesOutput += lineOfOutput.length();
}
//Close file
}

What is a buffer on java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a homework to do on Java and is asks me to create a buffer method and the constructor should make an empty buffer structure.
There is no details about what that buffer is. It also wants to insert chars inside the buffer, delete the char that buffer shows, go buffer X positions left or right and tell the number of buffers chars. All these with different methods.
The problem is WHAT IS THAT BUFFER??? Is this something specific?
I would just use a StringBuilder. There are many other possible solutions but StringBuilder is the most widely used buffer for chars these days.
StringBuilder sb = new StringBuidler();
It also wants to insert chars inside the buffer,
sb.append('!');
sb.append("Hello");
sb.insert(5, "bye");
delete the char that buffer shows,
sb.delete(3, 6);
go buffer X positions left or right and tell the number of buffers chars.
sb.charAt(5); // character at 5
int len = sb.length(); // number of characters.

Java: Need optimzed (fast) method for writing integer array to FileOutputStream [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Searching yields many questions about how to convert an int to byte[]. I have a project with a critical loop that writes a long int[] to a FileOutputStream. FileOutputStream requires a byte[] for writing. I can brute-force different methods; I'm looking for a way to send an int[] directly to a FileOutputStream or the fastest method to convert int[] to byte[] - something like wrapping a buffer. I see ways to wrap a byte[] to convert to int[] and float[]... but none the other way (from int[] to byte[]). Thanks.
Update: still hoping to avoid the complexity (or experimenting - for now) of memory mapped I/O until the need is proven. The comments below prompted me to look at creating a ByteBuffer, wrapping it in an IntBuffer, writing ints to the IntBuffer, then extracting a byte[] from the ByteBuffer to send to the FileOutputStream. The obvious alternative is just to use byte[] directly, which requires that I manipulate my data as bytes rather than ints, which I can do - but how much more efficient (if at all) is it compared to the byte[]/ByteBuffer/IntBuffer wrapping scheme?
Your bottleneck is most likely to be your disk IO so what you do in CPU doesn't matter. I would make sure you trying to solve a problem which will make a difference to your application.
If you have a fast disk sub system and you have short bursts of data, your CPU can matter and the fastest way to do the conversion is to avoid performing the conversion in the first place, ie don't use a byte[] at all. An example if OpenHFT/Java Chronicle this takes an int value and writes it direct to a memory mapped file memory region as a 32-bit value. This means each write consists of a single machine code instruction and takes about 1.5 ns on average.
Try ObjectOutputStream.writeObject(intArray). You can later read it with ObjectIntputStream.readObject

Java DataOutputStream object flushes after 8192 bytes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a Java variable declared as such:
RandomAccessFile file = new RandomAccessFile("path-to-file");
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file.getFD())));
The problem is that after 2048 introduced integers (or 8192 bytes), if I try to write another one, the output flushes to disk without any warning.
After some research I found out that DataOutputStream has a 64k limit, and I couldn't find anything official about BufferedOutputStream or FileOutputStream.
Can anybody please tell me which structure has this limitation and how can I increase it?
It would also be nice to find out why does the structure flush without being told to instead of just raising an exception?
It's the BufferedOutputStream who flushes, whose default size is 8192:
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
Just create the BufferedOutputStream with a seconds parameter describing the wanted buffer size!
int BUFFER_SIZE= <some value>;
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file.getFD()), BUFFER_SIZE));
However, this might not be what you want!
Queuing writes and writing them in one big chunk (at flush) is the very purpose of the BufferedOutputStream and it increases performance considerably. It's not at all a problem when it flushes. Just make sure that it flushes after you made your last write.
Additionally, I'm pretty sure you got the 64K limit of the DataOutputStream wrong!
Itself will write unlimited bytes, however it will only write strings with a maximum length of 64K when encoded as UTF-8.
It also contains a counter how many bytes have been written. Obviously this one will overflow after Integer.MAX_VALUE written bytes, but that shouldn't matter!

Categories