User Tools

Site Tools


java:byte_streams_to_data

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
java:byte_streams_to_data [2021/03/29 08:08]
gthanos [Μετασχηματισμός των ροών δυαδικών δεδομένων σε βασικούς τύπους δεδομένων]
java:byte_streams_to_data [2021/04/04 16:42]
gthanos [Μετασχηματισμός των ροών δυαδικών δεδομένων σε βασικούς τύπους δεδομένων]
Line 18: Line 18:
 import java.nio.*; import java.nio.*;
 import java.io.*; import java.io.*;
 +import java.util.Arrays;
  
 public class ByteBufferReadWriteTest { public class ByteBufferReadWriteTest {
Line 35: Line 36:
      
   public static void main(String []args ) {   public static void main(String []args ) {
-    File file = new File("my.bin");+    File file = new File("file.bin");
     write(file);     write(file);
     read(file);     read(file);
Line 43: Line 44:
     int a = -159954;     int a = -159954;
     double b = 125.128953;     double b = 125.128953;
-    char c = 'Θ';+    char c = 'θ';
     String str = "Πώς είσαι;";     String str = "Πώς είσαι;";
          
-    ByteBuffer buffer = ByteBuffer.allocate(34);+    ByteBuffer buffer = ByteBuffer.allocate(512); 
 +    buffer.order(ByteOrder.BIG_ENDIAN);
     buffer.putInt(a);     buffer.putInt(a);
     buffer.putDouble(b);     buffer.putDouble(b);
     buffer.putChar(c);     buffer.putChar(c);
     buffer.put(str.getBytes(java.nio.charset.StandardCharsets.UTF_8));     buffer.put(str.getBytes(java.nio.charset.StandardCharsets.UTF_8));
 +    int buffer_size = buffer.position();
          
     try(FileOutputStream out = new FileOutputStream(file)) {     try(FileOutputStream out = new FileOutputStream(file)) {
       byte []array = buffer.array();       byte []array = buffer.array();
 +      array = Arrays.copyOf(array, buffer_size);
       out.write(array);       out.write(array);
       // just for debugging purposes       // just for debugging purposes
-      // print_array(array);+      //print_array(array);
     }     }
     catch(IOException ex) {     catch(IOException ex) {
Line 69: Line 73:
          
     byte array[] = new byte[512];     byte array[] = new byte[512];
-    +    int read_size;
     try(FileInputStream in = new FileInputStream(file)) {     try(FileInputStream in = new FileInputStream(file)) {
-      in.read(array);+      read_size = in.read(array); 
 +      array = Arrays.copyOf(array, read_size);
     }     }
     catch(IOException ex) {     catch(IOException ex) {
Line 77: Line 82:
     }     }
     // just for debugging purposes     // just for debugging purposes
-    // print_array(array);+    //print_array(array);
          
     ByteBuffer buffer = ByteBuffer.wrap(array);     ByteBuffer buffer = ByteBuffer.wrap(array);
 +    buffer.order(ByteOrder.BIG_ENDIAN);
     System.out.println(buffer.getInt());     System.out.println(buffer.getInt());
     System.out.println(buffer.getDouble());     System.out.println(buffer.getDouble());
-    System.out.println(buffer.getChar()); +    System.out.println("'"+buffer.getChar()+"'"); 
-    byte [] str = new byte[18]; +    int str_size = buffer.remaining(); 
-    buffer.get(str); +    byte[] bytes = new byte[str_size]; 
-    System.out.println(new String(str, java.nio.charset.StandardCharsets.UTF_8)); +    buffer.get(bytes); 
-    +    System.out.println(new String(bytes, java.nio.charset.StandardCharsets.UTF_8));
   }   }
 } }
Line 92: Line 98:
  
 <WRAP tip 80% center round> <WRAP tip 80% center round>
-H //default// υλοποίηση ενός [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html|ByteBuffer]] καταχωρεί τα δεδομένα ή διαβάζει τα δεδομένα κατά big-endian. Εάν θέλετε να αλλάξετε τη σειρά καταχώρησης των δεδομένων στο buffer σε little-endian, μπορείτε να χρησιμοποιήσετε τη μέθοδο [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#order-java.nio.ByteOrder-|order]]. H μέθοδος λαμβάνει ένα αντικείμενο της κλάσης [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html|java.nio.ByteOrder]], το οποίο είναι ένα εκ των [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html#BIG_ENDIAN|java.nio.ByteOrder.BIG_ENDIAN]] ή [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html#BIG_ENDIAN|java.nio.ByteOrder.LITTLE_ENDIAN]]. +H //default// υλοποίηση ενός [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html|ByteBuffer]] καταχωρεί τα δεδομένα ή διαβάζει τα δεδομένα κατά big-endian. Εάν θέλετε να αλλάξετε τη σειρά καταχώρησης των δεδομένων στο buffer σε little-endian, μπορείτε να χρησιμοποιήσετε τη μέθοδο [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#order-java.nio.ByteOrder-|order]]. H μέθοδος λαμβάνει ένα αντικείμενο της κλάσης [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html|java.nio.ByteOrder]], το οποίο είναι ένα εκ των [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html#BIG_ENDIAN|java.nio.ByteOrder.BIG_ENDIAN]] ή [[https://docs.oracle.com/javase/8/docs/api/java/nio/ByteOrder.html#LITTLE_ENDIAN|java.nio.ByteOrder.LITTLE_ENDIAN]]. 
 Πληροφορίες για τη διαφορά μεταξύ big-endian και little-endian μπορείτε να βρείτε [[https://www.youtube.com/watch?v=seZLUbgbB7Y|στο βίντεο]]. Πληροφορίες για τη διαφορά μεταξύ big-endian και little-endian μπορείτε να βρείτε [[https://www.youtube.com/watch?v=seZLUbgbB7Y|στο βίντεο]].
 </WRAP> </WRAP>
java/byte_streams_to_data.txt · Last modified: 2021/04/04 15:44 (external edit)