java - What are these two extra bytes in an ObjectOutputStream file? -
i came across problem while working on question raised earlier.
this specific objectinputstream
, not binary reading in general, title may misleading.
basically problem there goes this: author has serialized hashmap of strings doubles. author's custom serialization format each entry in hashmap pretty simple
int n // length of string key 4-byte integer byte[n] key // string of length n double value // value associated key
now reason, during serialization process, 1 of strings 2010-00-008.html
serialized 2 bytes, shown here.
so instead of 16 bytes being written, 18 bytes written instead. bound cause problems because still says string 16 bytes long.
however, reason, can write hash map out , read in perfectly! seems given 18 byte string, can read 16 bytes , still read whole thing.
test code
here's code. it's code in other question except made should able change path , run it. after run it, you'll sequence of write statements followed sequence of read statements. inspect file , should notice bytes in string, program not crash.
import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.objectinputstream; import java.io.objectoutputstream; import java.util.hashmap; import java.util.map; public class test { // customize path needed public static string path = "c:\\temp\\sample.dat"; hashmap<string, double> map = new hashmap<string, double>(); public test() { map.put("2010-00-027.html",21732.994621513037); map.put("2010-00-020.html",3466.5169348296736); map.put("2010-00-051.html",12528.648992702407); map.put("2010-00-062.html",3354.8950010256385); map.put("2010-00-024.html",10295.095511718278); map.put("2010-00-052.html",5381.513344679818); map.put("2010-00-007.html",16466.33813960735); map.put("2010-00-017.html",9484.969198176652); map.put("2010-00-054.html",15423.873112634772); map.put("2010-00-022.html",8123.842752870753); map.put("2010-00-033.html",21238.496665104063); map.put("2010-00-028.html",7578.792651786424); map.put("2010-00-048.html",3566.4118233046393); map.put("2010-00-040.html",2681.0799941861724); map.put("2010-00-049.html",14308.090890746222); map.put("2010-00-058.html",5911.342406606804); map.put("2010-00-045.html",2284.118716145881); map.put("2010-00-031.html",2859.565771680721); map.put("2010-00-046.html",4555.187022907964); map.put("2010-00-036.html",8479.709295569426); map.put("2010-00-061.html",846.8292195815125); map.put("2010-00-023.html",14108.644025417952); map.put("2010-00-041.html",22686.232732684934); map.put("2010-00-025.html",9513.539663409734); map.put("2010-00-012.html",459.6427911376829); map.put("2010-00-005.html",0.0); map.put("2010-00-013.html",2646.403220496738); map.put("2010-00-065.html",5808.86423609936); map.put("2010-00-056.html",12154.250518054876); map.put("2010-00-008.html",10811.15198506469); map.put("2010-00-042.html",9271.006516004005); map.put("2010-00-000.html",4387.4162586468965); map.put("2010-00-059.html",4456.211623469774); map.put("2010-00-055.html",3534.7511584735325); map.put("2010-00-057.html",8745.640098512009); map.put("2010-00-032.html",4993.295735075575); map.put("2010-00-021.html",3852.5805998017922); map.put("2010-00-043.html",4108.020033536286); map.put("2010-00-053.html",2.2446400279239946); map.put("2010-00-030.html",17853.541210836203); } public void write() { try { objectoutputstream oos = new objectoutputstream(new fileoutputstream(path)); oos.writeint(map.size()); // write size of map (map.entry<string, double> entry : map.entryset()) { // iterate entries system.out.println("writing ("+ entry.getkey() +","+ entry.getvalue() +")"); byte[] bytes = entry.getkey().getbytes(); oos.writeint(bytes.length); // length of key string oos.write(bytes); // key string bytes oos.writedouble(entry.getvalue()); // value } oos.close(); } catch (exception e) { } } public void read() { try { fileinputstream f = new fileinputstream(path); objectinputstream ois = new objectinputstream(f); int size = ois.readint(); // read size of map hashmap<string, double> newmap = new hashmap<>(size); (int = 0; < size; i++) { // iterate entries int length = ois.readint(); // length of key string byte[] bytes = new byte[length]; ois.readfully(bytes, 0, length); //ois.read(bytes); string key = new string(bytes); double value = ois.readdouble(); // value newmap.put(key, value); system.out.println("read ("+ key +","+ value +")"); } } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { test t = new test(); t.write(); t.read(); } }
you need read protocol chapter of object serialization specification. stream full of type , block markers in addition actual data. 1 of them, , filtered out objectinputstream
when stream read correctly.
edit bytes 77 64
, means tc_block_data
of size 0x64.
Comments
Post a Comment