Java Code Examples for java.io.CharArrayWriter#append()

The following examples show how to use java.io.CharArrayWriter#append() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    Object value = null;
    if (null != request) {
        HttpSession sess = request.getSession(false);
        if (null != sess) {
            value = sess.getAttribute(header);
        }
    } else {
        value = "??";
    }
    if (value != null) {
        if (value instanceof String) {
            buf.append((String) value);
        } else {
            buf.append(value.toString());
        }
    } else {
        buf.append('-');
    }
}
 
Example 2
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    String value = null;
    if (requestAttributesEnabled) {
        Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE);
        if (addr == null) {
            value = request.getRemoteAddr();
        } else {
            value = addr.toString();
        }
    } else {
        value = request.getRemoteAddr();
    }

    if (ipv6Canonical) {
        value = IPv6Utils.canonize(value);
    }
    buf.append(value);
}
 
Example 3
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    if (request != null) {
        String method = request.getMethod();
        if (method == null) {
            // No method means no request line
            buf.append('-');
        } else {
            buf.append(request.getMethod());
            buf.append(' ');
            buf.append(request.getRequestURI());
            if (request.getQueryString() != null) {
                buf.append('?');
                buf.append(request.getQueryString());
            }
            buf.append(' ');
            buf.append(request.getProtocol());
        }
    } else {
        buf.append('-');
    }
}
 
Example 4
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    if (requestAttributesEnabled && portType == PortType.LOCAL) {
        Object port = request.getAttribute(SERVER_PORT_ATTRIBUTE);
        if (port == null) {
            buf.append(Integer.toString(request.getServerPort()));
        } else {
            buf.append(port.toString());
        }
    } else {
        if (portType == PortType.LOCAL) {
            buf.append(Integer.toString(request.getServerPort()));
        } else {
            buf.append(Integer.toString(request.getRemotePort()));
        }
    }
}
 
Example 5
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    // Don't need to flush since trigger for log message is after the
    // response has been committed
    long length = response.getBytesWritten(false);
    if (length <= 0) {
        // Protect against nulls and unexpected types as these values
        // may be set by untrusted applications
        Object start = request.getAttribute(
                Globals.SENDFILE_FILE_START_ATTR);
        if (start instanceof Long) {
            Object end = request.getAttribute(
                    Globals.SENDFILE_FILE_END_ATTR);
            if (end instanceof Long) {
                length = ((Long) end).longValue() -
                        ((Long) start).longValue();
            }
        }
    }
    if (length <= 0 && conversion) {
        buf.append('-');
    } else {
        buf.append(Long.toString(length));
    }
}
 
Example 6
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    Object value = null;
    if (request != null) {
        value = request.getAttribute(header);
    } else {
        value = "??";
    }
    if (value != null) {
        if (value instanceof String) {
            buf.append((String) value);
        } else {
            buf.append(value.toString());
        }
    } else {
        buf.append('-');
    }
}
 
Example 7
Source File: ExtendedAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    ElementTimestampStruct eds = currentDate.get();
    long millis = eds.currentTimestamp.getTime();
    if (date.getTime() > (millis + INTERVAL -1) ||
            date.getTime() < millis) {
        eds.currentTimestamp.setTime(
                date.getTime() - (date.getTime() % INTERVAL));
        eds.currentTimestampString =
            eds.currentTimestampFormat.format(eds.currentTimestamp);
    }
    buf.append(eds.currentTimestampString);
}
 
Example 8
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    Enumeration<String> iter = request.getHeaders(header);
    if (iter.hasMoreElements()) {
        buf.append(iter.nextElement());
        while (iter.hasMoreElements()) {
            buf.append(',').append(iter.nextElement());
        }
        return;
    }
    buf.append('-');
}
 
Example 9
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    if (request != null) {
        String value = request.getRemoteUser();
        if (value != null) {
            buf.append(value);
        } else {
            buf.append('-');
        }
    } else {
        buf.append('-');
    }
}
 
Example 10
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    if (request != null) {
        buf.append(request.getRequestURI());
    } else {
        buf.append('-');
    }
}
 
Example 11
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    String value = "-";
    Cookie[] c = request.getCookies();
    if (c != null) {
        for (int i = 0; i < c.length; i++) {
            if (header.equals(c[i].getName())) {
                value = c[i].getValue();
                break;
            }
        }
    }
    buf.append(value);
}
 
Example 12
Source File: LineSeparatorHarmonizer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String replaceLineSeparators(CharSequence content, String newLineSeparator) {
	CharArrayWriter writer = new CharArrayWriter(content.length());
	boolean isLookahead = false;
	char ignoreNext = '\u0000';
	for(int i=0; i<content.length(); ++i) {
		char c = content.charAt(i);
		if (isLookahead) {
			isLookahead = false;
			if (c == ignoreNext)
				continue;
		}
		switch (c) {
			case '\n':
				writer.append(newLineSeparator);
				isLookahead = true;
				ignoreNext = '\r';
				break;
			case '\r':
				writer.append(newLineSeparator);
				isLookahead = true;
				ignoreNext = '\n';
				break;
			default:
				writer.append(c);
		}
	}
	return writer.toString();
}
 
Example 13
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    buf.append(str);
}
 
Example 14
Source File: ExtendedAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    buf.append(wrap(request.getHeader(header)));
}
 
Example 15
Source File: JSONStringWriter.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
public static String toJSON(String value) {
  int valen = value.length();
  CharArrayWriter out = new CharArrayWriter(valen + 2);

  out.append('"');
  for (int i=0; i < valen; i++) {
    char ch = value.charAt(i);
    /* Per RFC 4627, only quotation mark, reverse solidus,
       and the control characters (U+0000 through U+001F)
       must be escaped.  Two-character sequence escape
       representations may be used for popular characters.
     */
    switch (ch) {
      case '"':
        out.append('\\');
        out.append('"');
        break;
      case '\\':
        out.append('\\');
        out.append('\\');
        break;
      case '\b':
        out.append('\\');
        out.append('b');
        break;
      case '\f':
        out.append('\\');
        out.append('f');
        break;
      case '\n':
        out.append('\\');
        out.append('n');
        break;
      case '\r':
        out.append('\\');
        out.append('r');
        break;
      case '\t':
        out.append('\\');
        out.append('t');
        break;
      default:
        // also matches '\u007F' through '\u009F' but these
        // characters may be escaped
        if (Character.isISOControl(ch)) {
          out.append(String.format("\\u%1$04x", (int) ch));
        } else {
          out.append(ch);
        }
        break;
    }
  }
  out.append('"');

  return out.toString();
}
 
Example 16
Source File: ExtendedAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    buf.append(wrap(request.getContext().getServletContext()
            .getAttribute(attribute)));
}
 
Example 17
Source File: JsonParser.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Get array chars up to given and include it.
 *
 * @return the char array.
 * @throws JsonException
 *         if JSON document has wrong format or i/o error occurs.
 */
private char[] nextString() throws JsonException {
    CharArrayWriter charArrayWriter = new CharArrayWriter();
    char c = nextAny(); // read '"'
    charArrayWriter.append(c);
    while (true) {
        switch (c = nextAny()) {
            case END_OF_STREAM:
            case '\n':
            case '\r':
                throw new JsonException("Syntax error. Unterminated string");
            case '\\':
                switch (c = nextAny()) {
                    case END_OF_STREAM:
                    case '\n':
                    case '\r':
                        throw new JsonException("Syntax error. Unterminated string");
                    case 'n':
                        charArrayWriter.append('\n');
                        break;
                    case 'r':
                        charArrayWriter.append('\r');
                        break;
                    case 'b':
                        charArrayWriter.append('\b');
                        break;
                    case 't':
                        charArrayWriter.append('\t');
                        break;
                    case 'f':
                        charArrayWriter.append('\f');
                        break;
                    case 'u': // unicode
                        charArrayWriter.append(readUnicodeCharacter());
                        break;
                    default:
                        charArrayWriter.append(c);
                        break;
                }
                break;
            default:
                charArrayWriter.append(c);
                if (c == '"') {
                    return charArrayWriter.toCharArray();
                }
                break;
        }
    }
}
 
Example 18
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    long timestamp = date.getTime();
    long frac;
    if (usesBegin) {
        timestamp -= time;
    }
    /*  Implementation note: This is deliberately not implemented using
     *  switch. If a switch is used the compiler (at least the Oracle
     *  one) will use a synthetic class to implement the switch. The
     *  problem is that this class needs to be pre-loaded when using a
     *  SecurityManager and the name of that class will depend on any
     *  anonymous inner classes and any other synthetic classes. As such
     *  the name is not constant and keeping the pre-loading up to date
     *  as the name changes is error prone.
     */
    if (type == FormatType.CLF) {
        buf.append(localDateCache.get().getFormat(timestamp));
    } else if (type == FormatType.SEC) {
        buf.append(Long.toString(timestamp / 1000));
    } else if (type == FormatType.MSEC) {
        buf.append(Long.toString(timestamp));
    } else if (type == FormatType.MSEC_FRAC) {
        frac = timestamp % 1000;
        if (frac < 100) {
            if (frac < 10) {
                buf.append('0');
                buf.append('0');
            } else {
                buf.append('0');
            }
        }
        buf.append(Long.toString(frac));
    } else {
        // FormatType.SDF
        String temp = localDateCache.get().getFormat(format, locale, timestamp);
        if (usesMsecs) {
            frac = timestamp % 1000;
            StringBuilder trippleMsec = new StringBuilder(4);
            if (frac < 100) {
                if (frac < 10) {
                    trippleMsec.append('0');
                    trippleMsec.append('0');
                } else {
                    trippleMsec.append('0');
                }
            }
            trippleMsec.append(frac);
            temp = temp.replace(trippleMsecPattern, trippleMsec);
            temp = temp.replace(msecPattern, Long.toString(frac));
        }
        buf.append(temp);
    }
}
 
Example 19
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    buf.append('-');
}
 
Example 20
Source File: AbstractSyslogMessageSender.java    From syslog-java-client with MIT License 4 votes vote down vote up
@Override
public void sendMessage(CharSequence message) throws IOException {
    CharArrayWriter writer = new CharArrayWriter();
    writer.append(message);
    sendMessage(writer);
}