public static String a(Readable readable) {
   StringBuilder stringBuilder = new StringBuilder();
   CharSequence allocate = CharBuffer.allocate(2048);
   while (true) {
     int read = readable.read(allocate);
     if (read == -1) {
       return stringBuilder.toString();
     }
     allocate.flip();
     stringBuilder.append(allocate, 0, read);
   }
 }
 /**
  * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not
  * close or flush either object.
  *
  * @param from the object to read from
  * @param to the object to write to
  * @return the number of characters copied
  * @throws IOException if an I/O error occurs
  */
 public static long copy(Readable from, Appendable to) throws IOException {
   checkNotNull(from);
   checkNotNull(to);
   CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
   long total = 0;
   while (from.read(buf) != -1) {
     buf.flip();
     to.append(buf);
     total += buf.remaining();
     buf.clear();
   }
   return total;
 }
Beispiel #3
0
 /**
  * Reads a line of text. A line is considered to be terminated by any one of a line feed ({@code
  * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a
  * linefeed ({@code "\r\n"}).
  *
  * @return a {@code String} containing the contents of the line, not including any
  *     line-termination characters, or {@code null} if the end of the stream has been reached.
  * @throws IOException if an I/O error occurs
  */
 public String readLine() throws IOException {
   while (lines.peek() == null) {
     cbuf.clear();
     // The default implementation of Reader#read(CharBuffer) allocates a
     // temporary char[], so we call Reader#read(char[], int, int) instead.
     int read = (reader != null) ? reader.read(buf, 0, buf.length) : readable.read(cbuf);
     if (read == -1) {
       lineBuf.finish();
       break;
     }
     lineBuf.add(buf, 0, read);
   }
   return lines.poll();
 }
 public static long copy(Readable paramReadable, Appendable paramAppendable)
   throws IOException
 {
   Preconditions.checkNotNull(paramReadable);
   Preconditions.checkNotNull(paramAppendable);
   CharBuffer localCharBuffer = CharBuffer.allocate(2048);
   long l = 0L;
   while (paramReadable.read(localCharBuffer) != -1)
   {
     localCharBuffer.flip();
     paramAppendable.append(localCharBuffer);
     l += localCharBuffer.remaining();
     localCharBuffer.clear();
   }
   return l;
 }
Beispiel #5
0
  /**
   * Parses the input as a .d Makefile as emitted by {@code gcc -MD} and returns the (target, [dep,
   * dep2, ...]) inside.
   */
  public static Depfile parseDepfile(Readable readable) throws IOException {
    String target = null;
    ImmutableList.Builder<String> prereqsBuilder = ImmutableList.builder();
    State state = State.LOOKING_FOR_TARGET;
    StringBuilder identifierBuilder = new StringBuilder();

    CharBuffer buffer = CharBuffer.allocate(4096);
    int numBackslashes = 0;

    while (readable.read(buffer) != -1) {
      buffer.flip();

      while (buffer.hasRemaining()) {
        char c = buffer.get();
        Action action = Action.NONE;
        boolean isBackslash = c == '\\';
        boolean isCarriageReturn = c == '\r';
        boolean isNewline = c == '\n';
        boolean isWhitespace = WHITESPACE_CHARS.indexOf(c) != -1;
        boolean inIdentifier = identifierBuilder.length() > 0;
        boolean isEscaped;
        if (state == State.LOOKING_FOR_TARGET) {
          isEscaped = ESCAPED_TARGET_CHARS.indexOf(c) != -1;
        } else {
          isEscaped = ESCAPED_PREREQ_CHARS.indexOf(c) != -1;
        }

        if (isBackslash) {
          // We need to count the number of backslashes in case the
          // first non-backslash is an escaped character.
          numBackslashes++;
        } else if (numBackslashes > 0 && isEscaped) {
          // Consume one backslash to escape the special char.
          numBackslashes--;
          if (inIdentifier) {
            action = Action.APPEND_TO_IDENTIFIER;
          }
        } else if (isWhitespace) {
          if (numBackslashes == 0) {
            if (state == State.FOUND_TARGET && inIdentifier) {
              action = Action.ADD_PREREQ;
            }
            if (state == State.FOUND_TARGET && (isNewline || isCarriageReturn)) {
              state = State.LOOKING_FOR_TARGET;
            }
          } else if (isNewline) {
            // Consume one backslash to escape \n or \r\n.
            numBackslashes--;
          } else if (!isCarriageReturn) {
            action = Action.APPEND_TO_IDENTIFIER;
          }
        } else if (c == ':' && state == State.LOOKING_FOR_TARGET) {
          state = State.FOUND_TARGET;
          action = Action.SET_TARGET;
        } else {
          action = Action.APPEND_TO_IDENTIFIER;
        }

        if (!isBackslash && numBackslashes > 0 && !isCarriageReturn) {
          int numBackslashesToAppend;
          if (isEscaped || isWhitespace) {
            // Backslashes escape themselves before an escaped character or whitespace.
            numBackslashesToAppend = numBackslashes / 2;
          } else {
            // Backslashes are literal before a non-escaped character.
            numBackslashesToAppend = numBackslashes;
          }

          for (int i = 0; i < numBackslashesToAppend; i++) {
            identifierBuilder.append('\\');
          }
          numBackslashes = 0;
        }

        switch (action) {
          case NONE:
            break;
          case APPEND_TO_IDENTIFIER:
            identifierBuilder.append(c);
            break;
          case SET_TARGET:
            if (target != null) {
              throw new HumanReadableException(
                  "Depfile parser cannot handle .d file with multiple targets");
            }
            target = identifierBuilder.toString();
            identifierBuilder.setLength(0);
            break;
          case ADD_PREREQ:
            prereqsBuilder.add(identifierBuilder.toString());
            identifierBuilder.setLength(0);
            break;
        }
      }

      buffer.clear();
    }

    ImmutableList<String> prereqs = prereqsBuilder.build();
    if (target == null || prereqs.isEmpty()) {
      throw new IOException("Could not find target or prereqs parsing depfile");
    } else {
      return new Depfile(target, prereqs);
    }
  }