Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package files;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.util.ArrayList;
- import java.util.List;
- public class Streams {
- /**
- * Read from an InputStream until a quote character (") is found, then read
- * until another quote character is found and return the bytes in between
- * the two quotes. If no quote character was found return null, if only one,
- * return the bytes from the quote to the end of the stream.
- *
- * @param in
- * @return A list containing the bytes between the first occurrence of a
- * quote character and the second.
- */
- public static List<Byte> getQuoted(InputStream in) throws IOException {
- List<Byte> result = new ArrayList<Byte>();
- boolean firstQuoteSeen = false;
- byte b;
- b = (byte) in.read();
- while (b != -1) {
- if (firstQuoteSeen && !(b == '"'))
- result.add(b); // if we already visited the first quote-
- // copy the byte
- else if (b == '"' && !firstQuoteSeen)
- firstQuoteSeen = true;
- else if (b == '"')
- return result; // second quote seen- thus, return the list
- b = (byte) in.read();
- }
- return result;
- }
- /**
- * Read from the input until a specific string is read, return the string
- * read up to (not including) the endMark.
- *
- * @param in
- * the Reader to read from
- * @param endMark
- * the string indicating to stop reading.
- * @return The string read up to (not including) the endMark (if the endMark
- * is not found, return up to the end of the stream).
- */
- public static String readUntil(Reader in, String endMark)
- throws IOException {
- StringBuilder result = new StringBuilder();
- String finalResult;
- int endMarkLength = endMark.length();
- String endOfResult;
- byte b;
- b = (byte) in.read();
- while (b != -1) {
- // check if the last chars of result are equal to endMark
- if (result.length() >= endMarkLength) {
- endOfResult = result.substring(result.length() - endMarkLength);
- if (endOfResult.equals(endMark)) {
- finalResult = result.substring(0,
- (result.length() - endMarkLength));
- return finalResult;
- }
- }
- result.append((char) b);
- b = (byte) in.read();
- }
- // the stream doesn't contain endMark- return the whole string
- return result.toString();
- }
- /**
- * Copy bytes from input to output, ignoring all occurrences of badByte.
- *
- * @param in
- * @param out
- * @param badByte
- */
- public static void filterOut(InputStream in, OutputStream out, byte badByte)
- throws IOException {
- byte b;
- b = (byte) in.read();
- while (b != -1) {
- if (b != badByte) {//copy only "good" bytes
- out.write(b);
- }
- b = (byte) in.read();
- }
- }
- /**
- * Read a 48-bit (unsigned) integer from the stream and return it. The
- * number is represented as five bytes, with the most-significant byte
- * first. If the stream ends before 5 bytes are read, return -1.
- *
- * @param in
- * @return the number read from the stream
- */
- public static long readNumber(InputStream in) throws IOException {
- byte[] bytes = new byte[5];
- long result=0;
- int success;
- success=in.read(bytes, 0, 5);
- if (success == -1) return -1;
- for (int i=0;i<5;i++){
- result=result<<8;
- result+=bytes[i];
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment