What is the best way to create byte data structure not fixed size in JAVA

user3357257

I need to create data structure, lets call it ByteCache, which containes some amount of bytes and it should support such methods:

1) ByteCache.length() - returns amount of bytes stored in it 2) ByteCache.add(Byte[] bytes) - adds new bytes to the end of presently contained inside 3) ByteCache.get(int offset, int length) - returns byte list from offset to offset+length bytes

It's supposed that there will be one thread writer (which adds bytes to cache) and another thread which reads some amounts of written bytes if there already present.

So what is the best way to do such things in java? May be there is such data structure, or some library ready to use, which I don't know, though I've read about some but didn't get a clue. I'm absolutely new to java, so please be patient.

Michael Tontchev

You can implement this with an ArrayList under the hood. ArrayList is an array that expands when more data is added than capacity permits.

Your ByteCache might look like

public class ByteCache {

    ArrayList<Byte> backing = new ArrayList<Byte>();

    public ByteCache(){
    }

    public ByteCache(Byte[] bytes){
        add(bytes);
    }

    public void add(Byte[] bytes){
        for(Byte b : bytes){
            backing.add(b);
        }
    }

    public int length(){
        return backing.size();
    }

    public Byte[] get(int offset, int length){
        if(offset < 0 || length < 1){
            return null;
        }

        Byte[] toRet = new Byte[length];

        for(int i = offset; i < offset + length; i++){
            if(i == backing.size()){
                break;
            }
            toRet[i - offset] = backing.get(i);
        }
        return toRet;
    }
}

You would need to implement your own get() and add() methods, but for length() a call to ArrayList's correct method should be enough.

P.S. ArrayList doesn't exactly expand - a new array is created that is double the size and all items are copied over.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Java, what is the best way to determine the size of an object?

What is the best way to create a data table in android?

What is the idiomatic way to create a fixed size std::array from a fixed size std::span?

What Java data structure is best for two-way multi-value mapping

What is the best way to create Statement and ResultSet in java

What is the best way to create smaller size layout for an activity

What is the best way of having a data structure that manages several services?

What is the better/best way of slicing Swift Data by size?

Best way to store a value into a Java data structure using columns and rows

What is the best data structure for storing a fixed length collection of named constants that can be used in a switch without explicit casting?

What Java Data Structure/Solution would best fit these requirements?

What is the best data structure for representing an upper triangular matrix in Java?

What is the best way to create an populate complex objects with data from api?

What is the proper way to create a Hybrid Data structure of Ternary Search Trees

What is the best way to convert a byte array to an IntStream?

What is the best way to merge 2 byte arrays?

What is the best way to store a byte array in memory

what is the best way to create primitive wrapper class in Java

What is the best way to create GUI for a crossword puzzle? java

What's the best way to design this structure in c?

What is the best way to convert structure to Json?

What is the best way to partially serialize a structure?

What is the best way to destruct the structure in C

What is the best way to structure this model/DB assiociations

What's the best way to structure the DOM?

What is the best way to structure a todo list database with Firestore to create a reference between projects and tasks?

What's the best way to persist data in a Java Desktop Application?

Java: What is the best way to write object data members to a file?

Best way to implement fixed size array in C++