I tried to ask this question earlier, but I was unclear in my question. Java BufferedReader action on character?
Here is my problem.. I have a BufferedReader set to read from a device. It is reading well. I have it set to
if (Status.reader.ready()) {
Lines = Status.reader.readLine();
}
if (Lines.contains(">")) {
log.level1("ready to send data")
}
Buffered reader does not report the > until I've sent more data to the device. The problem is that when reader contains > it is not reporting ready. It holds onto the > until I input more data.
I tried the following and it returns nothing. It does not even return the log.level0()
Lines = ""
try {
Lines = Status.reader.readLine();
} catch (IOException e) {
Log.level0("Attempted to read blank line");
}
Here is the actual data sent:
^M^M01 02 F3^M00 01 F3 3E^M>
But BufferedReader ignores the > until more data has been sent then get a result like this:
>0102
When I check the actual data from the device from the command prompt, it returns what I'd expect, the > is present.
BufferedReader will not give me the >. Is there some way I can check for this char otherwise?
The BufferedReader.readLine() method reads data a line at a time. That is, it will attempt to read characters until it sees an end-of-line sequence (e.g. "\n", "\r" or "\r\n") or the end of stream.
If your input data is not line oriented, then you should not be using readLine() to read it. I suggest that you do your own record / message extraction; e.g.
BufferedReader br = ...
StringBuilder sb = new StringBuilder(...);
int ch = br.read();
while (ch != -1 && ch != '>') {
sb.append((char) ch);
ch = br.read();
}
String record = sb.toString();
Check this:
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedReader.html
I recommend that you use the function public int read() instead.
At google you can find a lot of examples1
With those F3s in there it looks to me like your data isn't even character-oriented let alone line-oriented. Is your device really Unicode-compliant?
I would use a BufferedInputStream.
Related
I'm trying to read in a file and modify the text, but I need to keep new lines when doing so. For example, if I were to read in a file that contained:
This is some text.
This is some more text.
It would just read in as
This is some text.This is some more text.
How do I keep that space? I think it has something to do with the /n escape character. I've seen using BufferReader and FileReader, but we haven't learned that in my class yet, so is there another way? What I've tried is something like this:
if (ch == 10)
{
ch = '\n';
fileOut.print(ch);
}
10 is the ASCII table code for a new line, so I thought Java could recognize it as that, but it doesn't.
In Java 8:
You can read lines using:
List<String> yourFileLines = Files.readAllLines(Paths.get("your_file"));
Then collect strings:
String collect = yourFileLines.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(" "));
The problem is that you (possibly) want to read your file a line at a time, and then you want to write it back a line at a time (keeping empty lines).
The following source does that, it reads the input file one line at a time, and writes it back one line at a time (keeping empty lines).
The only problem is ... it possibly changes the new line, maybe you are reading a unix file and write a dos file or vice-versa depending on the system you are running in and the source type of the file you a reading.
Keeping the original newline can introduce a lot complexity, read BufferedReader and PrintWriter api docs for more information.
public void process(File input , File output){
try(InputStream in = new FileInputStream(input);
OutputStream out = new FileOutputStream(output)){
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"),true);
PrintWriter writer = new PrintWriter( new OutputStreamWriter(out,"utf-8"));
String line=null;
while((line=reader.readLine())!=null){
String processed = proces(line);
writer.println(processed);
}
} catch (IOException e) {
// Some exception management
}
}
public String proces(String line){
return line;
}
/n should be \n
if (ch == 10)
{
ch = '\n';
fileOut.print(ch);
}
Is that a typo?
ch = '/n';
otherwise use
ch = '\n';
I am writing a class that will read lines from a log file when it is updated.
I am using Apache VFS2 to get a method called when a file is updated. My main issue is I don't want to read the line from the file if the line is not complete yet, as in it does have a "\n" or "\r" line separator type character at the end. I think i have looked at all the Java libraries i can to read lines but they all discard the EOF and line termination information so I don't think I can use them.
Instead I am looking at reading it in byte by byte and then checking the result to then discard all stuff that comes after the last line separator. I was wondering what you folks thoughts on the best method for doing this is.
So for example:
2013-Jul-01_14:07:17.875 - Connection to Message Bus is reestablished<LF>
2013-Jul-01_14:07:17.875 - Connection to Message Bus is reestablished<LF>
2013-Jul-01_14:15:08.205 - No connection to Message Bus - reestablish before we can publish<LF>
2013-Jul-01_14:15:08.205 - NOT A REAL LINE PLEASE DONT READ
I want to read in the first 3 but not the fourth as it doesn't have a line feed or carriage return character ().
I have looked at Apache commons-io Tailer stuff but I cant tell if that will give me "incomplete" lines (and I realize I will have to ditch the VFS2 stuff to use it).
So psudo-code:
private void ingestFileObject(FileObject file) {
BufferedInputStream bs = new BufferedInputStream(file.getContent().getInputStream());
StringBuilder result = new StringBuilder();
while (bs.available() > 0) {
result.append((char) bs.read());
}
bs.close();
String resultString = result.toString();
//determine what part of resultString is after last carriage return/line seperate (using regex [\\r\\n]+?
//remove the offending part of String.
}
Or any other solutions completely ignoring my psudo-code are welcome at this point too...
Thanks
Is using Scanner help you?
Scanner scanner = new Scanner(file);
//block till there is some thing with a new line
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//do processing.
}
This is what I ended up doing:
BufferedReader bufReader = new BufferedReader(new InputStreamReader(file.getContent().getInputStream()));
StringBuilder result = new StringBuilder();
int readInInt = -1;
String charsSinceLastLineSep = "";
if (bufReader.ready()) {
while (-1 != (readInInt = bufReader.read())) {
char readInChar = (char) readInInt;
// if new line reset line buffer, otherwise add to buffer
if (readInChar == '\n' || readInChar == '\r') {
charsSinceLastLineSep = "";
} else {
charsSinceLastLineSep += readInChar;
}
result.append(readInChar);
}
bufReader.close();
// remove all characters added since last Carriage Return or NewLine was found indicating
// that line was not a complete log line
String resultString = (result.subSequence(0, (result.length() - charsSinceLastLineSep.length())).toString());
I am trying to read text from a web document using a BufferedReader over an InputStreamReader on an URL (to the file on some Apache server).
String result = "";
URL url = new URL("http://someserver.domain/somefile");
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1"));
result += in.readLine();
Now this works just fine. But Obviously I'd like the reader not to just read one line, but as many as there are in the file.
Looking at the BufferedReader API the following code should do just that:
while (in.ready()) {
result += in.readLine();
}
I.e. read all lines while there are more lines, stop when no more lines are there. This code does not work however - the reader just never reports ready() = true!
I can even print the ready() value right before reading a line (which reads the correct string from the file) but the reader will report 'false'.
Am I doing something wrong? Why does the BufferedReader return 'false' on ready when there is actually stuff to read?
ready() != has more
ready() does not indicate that there is more data to be read. It only shows if a read will could block the thread. It is likely that it will return false before you read all data.
To find out if there is no more data check if readLine() returns null.
String line = in.readLine();
while(line != null){
...
line = in.readLine();
}
Another way you can do this that bypasses the in.ready() is something like:
while ((nextLine = in.readLine()) != null) {
result += nextLine;
}
You will just continue reading until you are done. This way you do not need to worry about the problem with in.ready().
I think the standard way to write this is to just attempt to read the line and verify that it returned sometime. Something like this:
while ((String nextLine = in.readLine()) != null) {
//System.out.println(nextLine);
result += nextLine;
}
So you just continue to go until you get null returned from the stream. See here for extra information:
http://download.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
The BufferedReader.ready() method is behaving as specified:
The Reader.ready() javadoc says the following:
[Returns] true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Then the BufferedReader.ready() javadoc says the following:
Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
If you put these two together, it is clear that BufferedReader.ready() can return false in situations where are characters available. In short, you shouldn't rely on ready() to test for logical end-of-file or end-of-stream.
This is what we have been using consistently for years - not sure if it is the "standard" method. I'd like to hear comments about the pros and cons of using URL.openURLStream() directly, and if that is causing the OP's problems. This code works for both HTTP and HTTPS connections.
URL getURL = new URL (servletURL.toString() + identifier+"?"+key+"="+value);
URLConnection uConn = getURL.openConnection();
BufferedReader br = new BufferedReader (new
InputStreamReader (uConn.getInputStream()));
for (String s = br.readLine() ; s != null ; s = br.readLine()) {
System.out.println ("[ServletOut] " + s);
// do stuff with s
}
br.close();
Basically the BufferedReader.ready() method can be used for checking whether the underlying stream is ready for providing data to the method caller.... else we can wait the thread for some time till it becomes ready.
But the real problem is that after we completely read the data stream, it will throw false..
so we didn't know whether the stream is fully read OR underlying stream is busy....
If you want to use in.ready(), the following worked for me well:
for (int i = 0; i < 10; i++) {
System.out.println("is InputStreamReader ready: " + in.ready());
if (!in.ready()) {
Thread.sleep(1000);
} else {
break;
}
}
I have this code in Java:
InputStreamReader isr = new InputStreamReader(getInputStream());
BufferedReader ir = new BufferedReader(isr);
String line;
while ((line = ir.readLine()) != null) {
//do stuff with "line"
}
If the input stream contains the following: "hello\nhey\ryo\r\ngood-day", then line variable would be following on each iteration:
hello
hey
yo
good-day
I want to read one line at a time, but I want to keep the line-termination character(s):
hello\n
hey\r
yo\r\n
good-day
How can I do this? Is there a ready-made classes I can use?
--
Update:
Here's what I'm trying to do and why I need to keep the end-of-line character (and why the EOL character may be different).
I'm reading a POST request. They consists of pure text messages where the lines always end with \r\n (by the standard specification). However POST request may contain binary data, which may contain bytes that look like termination characters to Java Reader objects.
In my example, an image is being uploaded. The image data is sent on a single line. However, however the image's binary data contains bytes that the READER would interpret as "\n", "\r" or sometimes "\r\n" if those two bytes happens to be next to each other.
I have to read the POST request one line at a time because that's how it works. I suppose, I COULD read everything and then parse the whole thing. But that's not efficient, especially if a large file (say 1024 MiB) file is being uploaded.
If you want to read a HTTP POST request, I strongly suggest using BufferedInputStream.read() (not BufferedReader!) directly (without readLine-like intermediate abstractions), paying attention to all details manually, including the handling of CR and LF according to the HTTP RFC.
Here is my answer to your more specific question (how to implement exactly that readLine). This might not be the fastest solution, but it's time complexity is optimal, and it works:
import java.io.BufferedReader;
import java.io.IOException;
public class LineReader {
private int i = -2;
private BufferedReader br;
public OriginalLineReader(BufferedReader br) { this.br = br; }
public String readLine() throws IOException {
if (i == -2) i = br.read();
if (i < 0) return null;
StringBuilder sb = new StringBuilder();
sb.append((char)i);
if (i != '\r' && i != '\n') {
while (0 <= (i = br.read()) && i != '\r' && i != '\n') {
sb.append((char)i);
}
if (i < 0) return sb.toString();
sb.append((char)i);
}
if (i == '\r') {
i = br.read();
if (i != '\n') return sb.toString();
sb.append((char)'\n');
}
i = -2;
return sb.toString();
}
}
You won't find such a readLine built into Java. It's likely that you will find similar, but not exactly matching readLines in a third-party .jar file. My recommendation is just to use the one above, if you really need that feature.
I am building a simple client-server program , I have in main :
FTPClient ftp = new FTPClient("www.kernel.org");
ftp.getReply();
ftp.sendCommand("USER " + "anonymous");
ftp.getReply();
ftp.sendCommand("PASS " + "anonymous");
ftp.getReply();
String com="";
while (!com.equalsIgnoreCase("quit")){
System.out.println("Enter your Commands . or Enter quit");
BufferedReader Keyboard = new BufferedReader(new InputStreamReader(System.in));
com = Keyboard.readLine();
ftp.sendCommand((com));
ftp.getReply();
System.out.println("===============");
}
ftp.close();
the problem is in the getReply() function, this function is :
public void getReply() throws IOException {
String line="";
while (br.ready())
{
line = br.readline();
System.out.println(line);
System.out.flush();
}
}
br is a BufferedReader.Now all the problem is that when the program starts it doesn't show the welcome message from the Server until I press Enter or any command, when I Debug the program Step by Step every thing is working perfectly.So is the problem in the readline and I should use something else or what?
The problem is likely that the end of the server response does not contain a newline character. The BufferedReader's readLine method will block until a line of data is received, where "a line" consists of some characters followed by a newline character (or the end of the stream). Consequently, the readLine call will not return if no newline is received.
In this situation then, the BufferedReader isn't doing you any good. You'd be better off using the underlying Reader yourself, reading into an array and emitting the output as soon as it comes in, such as the following:
final char[] buffer = new char[256]; // or whatever size you want
int nRead;
while ((nRead = reader.read(buffer)) != -1)
{
System.out.println(new String(buffer, 0, nRead));
System.out.flush();
}
The condition in the while loop there might look confusing if you're not used to it before, but it combines the read operation (which reads into the buffer) with the check that the end of the stream has not been reached. Likewise, the construction of the String within the while loop takes into account the fact that the buffer may not have been filled entirely, so only as many characters as were supplied are used.
Note that this particular snippet keeps looping until the stream is empty; you may wish to add another exit condition in your particular case.