Buffered Writer occasionally creates symbols rather than numbers - java

So I have a program that collects a bunch of data and continuously concatenates the data into a string with a single white space between each entry. During my close routine I print the String into a txt file using buffered writer. About 50% of the time the data shows up as (mostly) Chinese symbols. Is the VM doing some weird Unicode stuff? Why does this only occur sometimes?
I've looked around on other forums and have not seen other instances of this problem. None of the other CS majors I know understand what is happening.
EDIT : the data is all integer numbers ranging 0-1365;
UPDATE: upon further research I found this which makes me think a may need a PrintStream rather than a BufferedWriter can anyone speak to that? I tested the PrintStream and I will not be able to construct it with a FileWriter as I would a BufferedWriter which means I need more research to write to my txt.
UPDATE: printing to the console does not make this error occur. I will accept an answer that explains way Notepad (the program I am using to open the txt) sometimes displays numbers and sometimes displays symbols.
Here is the relevant code:
//fields
private static BufferedWriter out;
private File saveFile;
String data;
//inside constructor
this.saveFile = new File("C:\\Users\\HPlaptop\\Desktop\\MouseData.txt");
this.saveFile.delete();
try{this.saveFile.createNewFile();}
catch (IOException e ){System.out.println("File creation error");}
try {out = new BufferedWriter(new FileWriter("C:\\Users\\HPlaptop\\Desktop\\MouseData.txt"));}
catch (IOException e) {System.out.println("IO Error");}
this.control.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ //there is a method call here but the basics are below
out.write(data);
out.close();
System.exit(0);
}
});
Here is an example data set printed correctly:
1365 767 1365 767 1365 767 1364 767 1353 756 1268 692 1114 604 980 488 812 334 744 283 694 244 593 150 473 81 328 13 207 0 124 0 115 0 102 0 99 6 107 13 132 20 173 32 187 31 190 25 194 20 201 17 215 14 221 10 224 7 224 7 224 7 226 6 226 6 226 6 226 6 226 6 226 6 226 6
This data set was taken seconds later and is not what I want
㐀ㄹ㈠㤰㐠㔸㈠㈱㐠㠶㈠㐱㐠㘲㈠㘰㌠㠷ㄠ㔹㌠㌳ㄠ㌹㈠㘹㈠㄰㈠㠷㈠㜳㈠㐶㈠㐷㈠㐶㈠㔷㈠㌶㈠㔵㈠㐵㈠㠰㈠㤴ㄠ㔲㈠㤴㐠‶㐲‹㌱㈠㘴〠㈠㘴〠㈠㘴〠㈠㜴〠㈠㠴〠㈠㠴〠㈠㜴㠠㈠㔴ㄠ‶㐲‵㤱㈠㔴ㄠ‹㐲‵㠱㈠㜴ㄠ‶㐲‹ㄱ㈠〵ㄠ‰㔲‰〱

The BufferedWriter is not making an error and the code is correct except for
the redundancy of using
this.saveFile.delete();
try{this.saveFile.createNewFile();}
catch (IOException e ){System.out.println("File creation error");}
and
new FileWriter
The error in reading the data occurs when the file is opened. The depending on what program opens the data different results are displayed because of the way the software reads the data. Notepad was displaying symbols because it interpreted the numbers as ASCII. The console did not try to interpret the data and just displayed what was written to it. Using a program that does not try to interpret the numbers in the file will allow the data to be viewed correctly.

Since you did not provide and example of what data you write into the stream, you are probably experiencing the bush hid the facts phenomenon.

Related

java - parsing custom file by tag

I have a file formated .TXT like this :
[Quest]
Name: The Adventure
Creator: Darius
Reward: 0g
[#New Monster]
MonsterID: 100
MonsterHP: 23987
MonsterLv: 11
MonsterDmg: 133
MonsterType: Magic
[#New Monster]
MonsterID: 101
MonsterHP: 21236
MonsterLv: 8
MonsterDmg: 95
MonsterType: Physical
[#New Item]
ItemID: 222
ItemDamage: 88
ItemType: Robe
How can i parse this file so i can get the monsterid,monsterhp etc and itemId,itemdamage etc. There is two [#New Monsters]

java com.itextpdf.text.exceptions.InvalidPdfException: The document has no page root

I'm trying to read a PDF files and I got this exception
com.itextpdf.text.exceptions.InvalidPdfException: The document has no page root (meaning: it's an invalid PDF).
at com.itextpdf.text.pdf.PdfReader.readPages(PdfReader.java:1248)
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:739)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:181)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
at com.mitech.med.watermark.Test2.main(Test2.java:11)
I used itext 5.5.10.
This is my code:
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
PdfReader reader = new PdfReader("C:/Users/matteo.fusi/Downloads/testPDF/1142.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
This is the link to the PDF document:
https://drive.google.com/file/d/0B2IrLGj9wefRVFZxSUhkN0o0N1k/view?usp=sharing
Thanks in advance
Regards
Matteo
I get the same issue on itext 5.5.10. I haven't taken a look yet on some new changes on the latest version. But it's working fine on itext 5.3.4. You could try on that version
The PDF in question is broken.
This is the page tree root dictionary object:
1 0 obj
<</Type /Pages /Count 1
/Kids[
4 0 R
]
/Type /Page
/MediaBox [ 0 0 595 842 ]
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Resources <<
/Font << /F0 6 0 R /F1 7 0 R /F2 8 0 R /F3 9 0 R /F4 10 0 R /F5 11 0 R /F6 12 0 R /F7 13 0 R /F8 14 0 R /F9 15 0 R /F10 16 0 R /F11 17 0 R /F12 18 0 R /F13 19 0 R /F14 20 0 R >>
/XObject <<
/Im0 5 0 R >>
>>
>>endobj
As you see the key Type occurs twice, once with value Pages and once with value Page. But the specification ISO 32000-1 clearly states in section 7.3.7 - Dictionary Objects:
Multiple entries in the same dictionary shall not have the same key.
(This, by the way, is a fairly obvious requirement for dictionary objects in general...)
The result of such a defect may be different in different PDF processors, the major obvious cases:
They might explicitly check for such problems and reject the file outright.
They might not check but use the first value assigned to the key.
They might not check but use the last value assigned to the key.
iText appears to be of the third kind. As far as iText is concerned, therefore, the page tree root dictionary has a Type value Page. But the specification requires the Type of a page tree node to be Pages. Thus, iText throws the observed exception.
Firstly I would try an alternative that seems to work for a lot of people :
http://pdfbox.apache.org/ <- Inspired by an older post I researchd on stack overflow
Secondly while debugging the issue I found this
rootPages == null || (!PdfName.PAGES.equals(rootPages.get(PdfName.TYPE))
&& !PdfName.PAGES.equals(rootPages.get(new PdfName("Types"))))
Is not satisfied hence your problem. I believe it might be a current Bug in Itext.
DFTBA

Android build error: ro.build.fingerprint cannot exceed 91 bytes

I'm building an android rom from the android source code but after about 5 minutes it gives this error.
error: ro.build.fingerprint cannot exceed 91 bytes: Android/mini_emulator_x86/mini-emulator-x86:5.0.555/AOSP/username02280306:userdebug/test-keys (97)
make: *** [out/target/product/mini-emulator-x86/system/build.prop] Error 1
make: *** Deleting file `out/target/product/mini-emulator-x86/system/build.prop'
make: *** Waiting for unfinished jobs....
How do I increase the ro.build.fingerprint size limit?
Plus I'm building on a Mac.
Edit build/tools/post_process_props.py. Change lines as follows:
PROP_NAME_MAX = 31
# PROP_VALUE_MAX = 91
PROP_VALUE_MAX = 128
Edit bionic/libc/include/sys/system_properties.h. Change lines as follows:
#define PROP_NAME_MAX 32
// #define PROP_VALUE_MAX 92
#define PROP_VALUE_MAX 128
Do
make clean
make
You can also run the second make command in parallel using syntax such as
make -j8
Alternatively, you can specify the build fingerprint string as command line argument to make using:
make -j5 BUILD_FINGERPRINT="....."
This will allow you to stay within the 91 byte limit.

Executing an exe and reading the OutputStream

I have a program called FFprobe which probes media (files/live streams ... ) and outputs result in different formats , for example :
ffprobe.exe -i test.ts -print_format xml -show_programs
gives this output :
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
<programs>
<program program_id="1201" program_num="1201" nb_streams="2" pmt_pid="1000" pcr_pid="2011" start_pts="45394227044" start_time="45394.227044">
<tag key="service_name" value="Arabica TV"/>
<tag key="service_provider" value="Nilesat"/>
<streams>
<stream index="10" codec_name="mpeg2video" codec_long_name="MPEG-2 video" profile="Main" codec_type="video" codec_time_base="1/50" codec_tag_string="[2][0][0][0]" codec_tag="0x0002" width="720" height="576" has_b_frames="1" sample_aspect_ratio="16:15" display_aspect_ratio="4:3" pix_fmt="yuv420p" level="8" timecode="08:28:54:09" id="0x7db" r_frame_rate="25/1" avg_frame_rate="25/1" time_base="1/90000" start_pts="4085542516" start_time="45394.916844" max_bit_rate="2348000">
<disposition default="0" dub="0" original="0" comment="0" lyrics="0" karaoke="0" forced="0" hearing_impaired="0" visual_impaired="0" clean_effects="0" attached_pic="0"/>
</stream>
<stream index="4" codec_name="mp2" codec_long_name="MP2 (MPEG audio layer 2)" codec_type="audio" codec_time_base="1/48000" codec_tag_string="[0][0][0][0]" codec_tag="0x0000" sample_fmt="s16p" sample_rate="48000" channels="2" channel_layout="stereo" bits_per_sample="0" id="0xbcf" r_frame_rate="0/0" avg_frame_rate="0/0" time_base="1/90000" start_pts="4085480434" start_time="45394.227044" bit_rate="384000">
<disposition default="0" dub="0" original="0" comment="0" lyrics="0" karaoke="0" forced="0" hearing_impaired="0" visual_impaired="0" clean_effects="0" attached_pic="0"/>
</stream>
</streams>
</program>
... more programs
to retrieve this info in java i used ProcessBuilder and a scanner , and then id write to a file once the result is ok ... but it wasnt :
Process proc = new ProcessBuilder("ffprobe.exe","-i", ... same arguments );
Scanner sc = new Scanner (proc.getInputStream()) // im 100% sure its not errorStream
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
the app just hangs with no output , i know its hanging cuz the process is still running and scanner has next , but , i don't know why it would do that ?If i execute the same in cmd i would get good result and ofc i can write to file with ">"
Ive tried it w/o the -print_format option , which gives the info in a plain text on the errorstream(i know its error cuz i was able to write with 2> not >> ) , and i was able to read the error stream in java , but its not meant for parsing cuz very very un-organized .
Input #0, mpegts, from 'PBR_REC_20140426094852_486.ts':
Duration: 00:13:34.30, start: 7791.344722, bitrate: 42154 kb/s
Program 1201
Metadata:
service_name : Arabica TV
service_provider: Nilesat
Stream #0:19[0x7db]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max. 2348 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:3[0xbcf]: Audio: mp2, 48000 Hz, stereo, s16p, 384 kb/s
Program 1202
Metadata:
service_name : NBN
service_provider: NILESAT
Stream #0:10[0x7d1]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max. 2600 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:11[0xbba](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 125 kb/s
Program 1203
Metadata:
service_name : Heya TV
service_provider: NILESAT
Stream #0:5[0x7d2]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max. 2600 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:6[0xbbc](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, mono, s16p, 125 kb/s
Program 1204 ... more programs
now it might seem organized and "parse-able" and i actually made a parser that worked to some point , but sometimes it doesnt stick to this structure and ruins the whole parsing, its why i need a xml/json ...
The -print_format im sure outputs to outputstream.
Im not asking for help on how to use FFprobe as thats not this place's purpose , Im asking why am i not being able to get the output stream from java while it is definitely outputting if i execute in windows .
I also tried apache commons-exec as i know processbuilder can be a pain ,it did execute perfectly and showed in System.in (black for intput and red for error), but getting the stream with apache is something i couldn't understand , i tried this example
The xml parser i already taken care of , simply put i just need to execute that first command from java and read the output , but for some reason its not working .
I generally use variations of this utility method:
public static void runProcess(ProcessBuilder pb) throws IOException {
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
Even if you don't want to print the error stream you need to consume it otherwise it will block the process. In which case you would need to consume it in a separate thread and only print the input stream from the main thread.

Java: why are multiple objects showing up with runhprof output?

I was curious about the runhprof output? I am mainly concerned about the memory section. It looks like there are multiple entries of the same class. Why would that be.
Is there a way to get hprof to print how much memory a particular class(the instances of that class) take up in memory. One value for each class.
Also, what tools do you use beside 'hat' to analyze the output?
I ran the java command with jvm arg:
-Xrunhprof:heap=sites,depth=4,format=a,file=prof/hprof_dump.txt
Here is brief snippet of the output. Some classes are listed multiple times in the output.
SITES BEGIN (ordered by live bytes) Tue Jul 28 19:33:41 2009
percent live alloc'ed stack class
rank self accum bytes objs bytes objs trace name
1 29.75% 29.75% 700080 43755 576000016 36000001 307483 java.lang.Double
2 7.13% 36.88% 167840 5245 370432 11576 300993 clojure.lang.PersistentHashMap$LeafNode
3 2.09% 38.98% 49296 2054 60048 2502 301295 clojure.lang.Symbol
4 2.09% 41.07% 49200 3 49200 3 301071 char[]
5 1.33% 42.40% 31344 1306 68088 2837 300998 clojure.lang.PersistentHashMap$BitmapIndexedNode
6 1.10% 43.50% 25800 645 25800 645 301050 clojure.lang.Var
7 1.05% 44.54% 24624 3 24624 3 301069 byte[]
8 0.86% 45.40% 20184 841 49608 2067 301003 clojure.lang.PersistentHashMap$INode[]
9 0.78% 46.18% 18304 572 58720 1835 301308 clojure.lang.PersistentList
10 0.75% 46.93% 17568 549 17568 549 308832 java.lang.String[]
11 0.70% 47.62% 16416 2 16416 2 301036 byte[]
Eclipse Memory Analyzer is excellent. Loads the dump file up very very quickly, produces lots of nice reports about the heapdump, lets you query the dump for objects/classes using a SQL-like language. Love it.

Categories

Resources