Je construis une classe qui gère un De Binary/Serialization. La méthode open()
reçoit un InputStream
et un OutputStream
. Ceux-ci sont créés par une autre méthode open()
qui reçoit un chemin en tant qu'argument. Le InputStream
est en fait un ByteArrayInputStream
. J'ai déjà fait quelques tests pour prouver que le InputStream
arrive à la méthode open()
avec du contenu - et c'est effectivement le cas. Mais quand j'essaye de mettre un ObjectInputStream
en l'employant, cela ne fonctionne pas. Aucune exception n'est levée, mais quand j'essaie de lire des octets, cela me donne toujours -1
.ObjectInputStream n'a pas d'octets disponibles après avoir été construit avec un ByteArrayInputStream
classe BinaryStrategy
public class BinaryStrategy implements SerializableStrategy{
public BinaryStrategy(){
try{
open("products.ser");
}catch(IOException ioe){
}
}
@Override
public void open(InputStream input, OutputStream output) throws IOException {
try{
this.ois = new ObjectInputStream(input);
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
@Override
public void writeObject(fpt.com.Product obj) throws IOException {
oos.writeObject(obj);
oos.flush();
}
@Override
public Product readObject() throws IOException {
Product read = new Product();
try{
read.readExternal(ois);
}catch(IOException | ClassNotFoundException exc){
System.out.println(exc);
}
return read;
}
}
SerializableStrategy d'interface (seulement la méthode par défaut)
default void open(Path path) throws IOException {
if (path != null) {
ByteArrayInputStream in = null;
if (Files.exists(path)) {
byte[] data = Files.readAllBytes(path);
in = new ByteArrayInputStream(data);
}
OutputStream out = Files.newOutputStream(path);
open(in, out);
}
Classe de produit
public class Product implements java.io.Externalizable {
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeLong(getId());
out.writeObject(getName());
out.writeObject(getPrice());
out.writeObject(getQuantity());
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.setId((Long)in.readLong());
this.setName((String) in.readObject());
this.setPrice((Double) in.readObject());
this.setQuantity((Integer) in.readObject());
}
je dû personnaliser parce que les attributs sont SimpleProperty
s
A public void open(InputStream input, OutputStream output)
j'ai essayé de faire des choses comme suit pour tester:
public void open(InputStream input, OutputStream output) throws IOException {
try{
System.out.println(input.available() + " " + input.read() + " " + input.read());
//is gives me: 181 172 237
//181 is the exact size of the file I have, so i think that the Output is ok
//172 237 - just some chars that are in the file
//I know that for now on it is going to give me an excepetion because
// of the position of the index that is reading. I did it just to test
this.ois = new ObjectInputStream(input);
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
Et puis l'autre test:
public void open(InputStream input, OutputStream output) throws IOException {
try{
this.ois = new ObjectInputStream(input);
System.out.println(ois.available() + " " + ois.read());
//here is where I am receiving -1 and 0 available bytes!
//so something is going wrong right here.
//i tried to just go on and try to read the object,
//but I got a EOFException, in other words, -1.
}catch(Exception ioe){
System.out.println(ioe);
}
this.oos = new ObjectOutputStream(output);
}
nous dans le reste du code, il n'y a rien que nous pouvons faire avec seulement ce que je –
édité maintenant. –
s'il vous plaît nous montrer le code où vous obtenez -1 également –