Hallo, bin noch neu hier also bitte nicht überstürzen 
Habe mir Eine eigene Array List für Primitive Datentypen gebastelt, nun habe ich das Problem dass ich zwar in einer 'Abgesonderten Umgebung' Keine Probleme mit dieser habe, allerdings in dieser in welcher ich diese brauch. Un zwar wird bei der addByte() aus irgendeinem Grund ein Object überschrieben und ich sitze hier schon seit stunden und Find einfach nicht die Lösung
.
Die unwichtigen teile habe ich durch // NOT RELEVANT ersetzt da diese nichts mit dem Problem zu tuhen haben:
PrimitiveArrayList - Klasse
Die Klasse in welcher ich diese (fehlerhaft?!) nutze:
Output:
Danke schon mal für eure Hilfe, kann den Fehler einfach nicht finden....
PS: Hab eine eigene Library, die Methode ArrayUtils.formatArray() gibt nur ein Array von und bis zu einer bestimmten Position formatiert als String zurück, machen tut die in dem sinne gar nix
Habe mir Eine eigene Array List für Primitive Datentypen gebastelt, nun habe ich das Problem dass ich zwar in einer 'Abgesonderten Umgebung' Keine Probleme mit dieser habe, allerdings in dieser in welcher ich diese brauch. Un zwar wird bei der addByte() aus irgendeinem Grund ein Object überschrieben und ich sitze hier schon seit stunden und Find einfach nicht die Lösung
Die unwichtigen teile habe ich durch // NOT RELEVANT ersetzt da diese nichts mit dem Problem zu tuhen haben:
PrimitiveArrayList - Klasse
Java:
package sr.list;
import java.util.ArrayList;
/***
* @version 0.1
* @author Simon Reinisch
*/
public class PrimitiveArrayList {
private final ArrayList<Object> CONTAINER = new ArrayList<>();
/**
* Create an Arraylist Wich can contains Primitive Arrays.
*/
public PrimitiveArrayList() {}
/**
* Add an Array to this list.Is synchronized.
*
* @param a - The Array.
*/
public synchronized void add(byte[] a) {
CONTAINER.add(new ByteWrapper(a));
}
public synchronized void add(int[] a) {
CONTAINER.add(new IntegerWrapper(a));
}
public synchronized void add(long[] a) {
CONTAINER.add(new LongWrapper(a));
}
public synchronized void add(double[] a) {
CONTAINER.add(new DoubleWrapper(a));
}
public synchronized void add(float[] a) {
CONTAINER.add(new FloatWrapper(a));
}
public synchronized void add(short[] a) {
CONTAINER.add(new ShortWrapper(a));
}
public synchronized void add(char[] a) {
CONTAINER.add(new CharacterWrapper(a));
}
/**
* Return the element on the given position. Is synchronized.
*
* @param index - The index of the element.
* @return The element.
*/
public synchronized byte[] getByte(int index) {
return ((ByteWrapper) CONTAINER.get(index)).DATA;
}
public synchronized int[] getInt(int index) {
return ((IntegerWrapper) CONTAINER.get(index)).DATA;
}
public synchronized long[] getLong(int index) {
return ((LongWrapper) CONTAINER.get(index)).DATA;
}
public synchronized double[] getDouble(int index) {
return ((DoubleWrapper) CONTAINER.get(index)).DATA;
}
public synchronized float[] getFloat(int index) {
return ((FloatWrapper) CONTAINER.get(index)).DATA;
}
public synchronized short[] getShort(int index) {
return ((ShortWrapper) CONTAINER.get(index)).DATA;
}
public synchronized char[] getChar(int index) {
return ((CharacterWrapper) CONTAINER.get(index)).DATA;
}
/**
* Removes the element at the specified position in this list. Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Is synchronized.
*
* @param index - The index of the element to be removed
* @return The removed element.
*/
public synchronized byte[] removeByte(int index) {
return ((ByteWrapper) CONTAINER.remove(index)).DATA;
}
public synchronized int[] removeInt(int index) {
return ((IntegerWrapper) CONTAINER.remove(index)).DATA;
}
public synchronized long[] removeLong(int index) {
return ((LongWrapper) CONTAINER.remove(index)).DATA;
}
public synchronized double[] removeDouble(int index) {
return ((DoubleWrapper) CONTAINER.remove(index)).DATA;
}
public synchronized float[] removeFloat(int index) {
return ((FloatWrapper) CONTAINER.remove(index)).DATA;
}
public synchronized short[] removeShort(int index) {
return ((ShortWrapper) CONTAINER.remove(index)).DATA;
}
/**
* Clear the list.
*/
public void clear() {
CONTAINER.clear();
}
/**
* Return the size of the list. Is synchronized.
*
* @return The size.
*/
public synchronized int size() {
return CONTAINER.size();
}
private class ByteWrapper {
public byte[] DATA;
public ByteWrapper(byte[] data) {
this.DATA = data;
}
}
private class IntegerWrapper {
public int[] DATA;
public IntegerWrapper(int[] data) {
this.DATA = data;
}
}
private class LongWrapper {
public long[] DATA;
public LongWrapper(long[] data) {
this.DATA = data;
}
}
private class DoubleWrapper {
public double[] DATA;
public DoubleWrapper(double[] data) {
this.DATA = data;
}
}
private class FloatWrapper {
public float[] DATA;
public FloatWrapper(float[] data) {
this.DATA = data;
}
}
private class ShortWrapper {
public short[] DATA;
public ShortWrapper(short[] data) {
this.DATA = data;
}
}
private class CharacterWrapper {
public char[] DATA;
public CharacterWrapper(char[] data) {
this.DATA = data;
}
}
}
Java:
package sr.net;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import sr.list.PrimitiveArrayList;
import sr.utils.ArrayUtils;
/***
* @version 0.2
* @author Simon Reinisch
*/
public class SocketInputStream {
private final PrimitiveArrayList RAW_PACKAGES = new PrimitiveArrayList();
private final HashMap<Byte, SocketStreamPackage> SSP_LIST = new HashMap<>();
private final HashMap<Byte, Channel> CHANNELS = new HashMap<>();
private final Object CHANNELS_LOCK = new Object();
private final int BUFFER_SIZE = 255; // 255 is max, 4 is min.
private final InputStream SOCKET_IN;
private final Socket SOCKET;
private boolean mRunning = true;
public SocketInputStream(Socket sock) throws IOException {
this.SOCKET = sock;
this.SOCKET_IN = sock.getInputStream();
listen();
handling();
}
/**
* Listen for new Packages.
*/
private void listen() {
Runnable ruu = () -> {
byte[] pack = new byte[BUFFER_SIZE];
while (mRunning) {
try {
SOCKET_IN.read(pack);
} catch (IOException e) {
e.printStackTrace();
break;
}
handle(pack);
} // End of while loop.
};
new Thread(ruu).start();
}
/**
* Handle the RAW_PACKAGES List.
*/
private void handling() {
Runnable ruu = () -> {
while (mRunning) {
byte[] pack, data;
byte channel, id, typ, length;
if (RAW_PACKAGES.size() > 100) {
pack = RAW_PACKAGES.removeByte(0);
channel = pack[0];
id = pack[1];
typ = pack[2];
length = pack[3];
data = Arrays.copyOfRange(pack, 4, length);
// TODO: DEBUG ->
System.out.println("[HANDLING] C=" + channel + " ID=" + id + " TYP=" + typ);
switch (typ) {
// NOT RELEVANT
} // End of switch.
} else {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
} // End of if.
} // End of while loop.
};
new Thread(ruu).start();
}
// Give the pacakge to the respective Channel.
private void handleOutput(SocketStreamPackage ssp) {
Runnable ruu = () -> {
synchronized (CHANNELS_LOCK) {
if (!CHANNELS.containsKey(ssp.CHANNEL_ID)) {
new Exception("Input on Channel with the ID " + ssp.CHANNEL_ID + " but there is no Channel with the ID" + ssp.CHANNEL_ID).printStackTrace();
} else {
CHANNELS.get(ssp.CHANNEL_ID).onReceive(ssp);
}
}
};
new Thread(ruu).start();
}
/**
* Create an new channel over wich you can talk.
*
* @param id - The id, must be unique and under 255.
* @param c - The Channel on wich are the bytes send.
*/
public void openChannel(byte id, Channel c) {
synchronized (CHANNELS_LOCK) {
CHANNELS.put(id, c);
}
}
/**
* Add a array to the handling list.
*/
public void handle(byte[] b) {
// DEBUG:
System.out.println("[HANDLE] >>" + ArrayUtils.formatArray(b, 0, 5));
this.RAW_PACKAGES.add(b);
// DEBUG:
for (int i = 0; i < RAW_PACKAGES.size(); i++) {
System.out.println("[HANDLE-CHECK] " + i + " >> " + ArrayUtils.formatArray(RAW_PACKAGES.getByte(i), 0, 5));
}
}
/**
* Close the Stream.
*
* @throws IOException
*/
public void close() throws IOException {
this.mRunning = false;
this.SOCKET.close();
}
@FunctionalInterface
public interface Channel {
/**
* @param ssp - The package.
*/
public abstract void onReceive(SocketStreamPackage ssp);
}
}
Code:
[HANDLE] >>[2, 1, 0, 52, 70] << Stimmt (weiß ich)
[HANDLE-CHECK] 0 >> [2, 1, 0, 52, 70] << Stimmt
[HANDLE] >>[2, 2, 0, 11, 53] << Stimmt (weiß ich)
[HANDLE-CHECK] 0 >> [2, 2, 0, 11, 53] << Warum wurde das überschrieben??? Da sollte [2, 1, 0, 52, 70] stehen
[HANDLE-CHECK] 1 >> [2, 2, 0, 11, 53] << Stimmt
Danke schon mal für eure Hilfe, kann den Fehler einfach nicht finden....
PS: Hab eine eigene Library, die Methode ArrayUtils.formatArray() gibt nur ein Array von und bis zu einer bestimmten Position formatiert als String zurück, machen tut die in dem sinne gar nix