Guest User

Untitled

a guest
Apr 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package files;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.Reader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class Streams {
  11. /**
  12. * Read from an InputStream until a quote character (") is found, then read
  13. * until another quote character is found and return the bytes in between
  14. * the two quotes. If no quote character was found return null, if only one,
  15. * return the bytes from the quote to the end of the stream.
  16. *
  17. * @param in
  18. * @return A list containing the bytes between the first occurrence of a
  19. * quote character and the second.
  20. */
  21. public static List<Byte> getQuoted(InputStream in) throws IOException {
  22. List<Byte> result = new ArrayList<Byte>();
  23. boolean firstQuoteSeen = false;
  24. byte b;
  25.  
  26. b = (byte) in.read();
  27. while (b != -1) {
  28. if (firstQuoteSeen && !(b == '"'))
  29. result.add(b); // if we already visited the first quote-
  30. // copy the byte
  31. else if (b == '"' && !firstQuoteSeen)
  32. firstQuoteSeen = true;
  33. else if (b == '"')
  34. return result; // second quote seen- thus, return the list
  35.  
  36. b = (byte) in.read();
  37.  
  38. }
  39. return result;
  40. }
  41.  
  42. /**
  43. * Read from the input until a specific string is read, return the string
  44. * read up to (not including) the endMark.
  45. *
  46. * @param in
  47. * the Reader to read from
  48. * @param endMark
  49. * the string indicating to stop reading.
  50. * @return The string read up to (not including) the endMark (if the endMark
  51. * is not found, return up to the end of the stream).
  52. */
  53. public static String readUntil(Reader in, String endMark)
  54. throws IOException {
  55. StringBuilder result = new StringBuilder();
  56. String finalResult;
  57. int endMarkLength = endMark.length();
  58. String endOfResult;
  59. byte b;
  60.  
  61. b = (byte) in.read();
  62. while (b != -1) {
  63. // check if the last chars of result are equal to endMark
  64. if (result.length() >= endMarkLength) {
  65. endOfResult = result.substring(result.length() - endMarkLength);
  66. if (endOfResult.equals(endMark)) {
  67. finalResult = result.substring(0,
  68. (result.length() - endMarkLength));
  69. return finalResult;
  70. }
  71. }
  72. result.append((char) b);
  73. b = (byte) in.read();
  74. }
  75. // the stream doesn't contain endMark- return the whole string
  76. return result.toString();
  77. }
  78.  
  79. /**
  80. * Copy bytes from input to output, ignoring all occurrences of badByte.
  81. *
  82. * @param in
  83. * @param out
  84. * @param badByte
  85. */
  86. public static void filterOut(InputStream in, OutputStream out, byte badByte)
  87. throws IOException {
  88. byte b;
  89.  
  90. b = (byte) in.read();
  91. while (b != -1) {
  92. if (b != badByte) {//copy only "good" bytes
  93. out.write(b);
  94. }
  95. b = (byte) in.read();
  96. }
  97. }
  98.  
  99. /**
  100. * Read a 48-bit (unsigned) integer from the stream and return it. The
  101. * number is represented as five bytes, with the most-significant byte
  102. * first. If the stream ends before 5 bytes are read, return -1.
  103. *
  104. * @param in
  105. * @return the number read from the stream
  106. */
  107. public static long readNumber(InputStream in) throws IOException {
  108. byte[] bytes = new byte[5];
  109. long result=0;
  110. int success;
  111. success=in.read(bytes, 0, 5);
  112. if (success == -1) return -1;
  113. for (int i=0;i<5;i++){
  114. result=result<<8;
  115. result+=bytes[i];
  116. }
  117. return result;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment