UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut); int lastHeader = headers.size() - 1; if (verbose) { pad(new Table.Cell(header.display, table.findHeaderByName(header.name)), width[col], request, out, isLastColumn); if (!isLastColumn) { out.append(" "); out.append("\n"); pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn); if (!isLastColumn) { out.append(" "); out.append("\n"); out.close(); return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
/** * Closes and {@link #reset resets} this writer for reuse. * * @throws IOException if an I/O error occurs */ @Override public void close() throws IOException { if (_outputStream != null) { flushBuffer(); _outputStream.close(); reset(); } }
/** * Writes a portion of an array of characters. * * @param cbuf the array of characters. * @param off the offset from which to start writing characters. * @param len the number of characters to write. * @throws IOException if an I/O error occurs. */ @Override public void write(char cbuf[], int off, int len) throws IOException { final int off_plus_len = off + len; for (int i = off; i < off_plus_len; ) { char c = cbuf[i++]; if (c < 0x80) { _bytes[_index] = (byte) c; if (++_index >= _bytes.length) { flushBuffer(); } } else { write(c); } } }
/** * Writes a character given its 31-bits Unicode. * * @param code the 31 bits Unicode of the character to be written. * @throws IOException if an I/O error occurs. */ @Override public void write(int code) throws IOException { if ((code & 0xffffff80) == 0) { _bytes[_index] = (byte) code; if (++_index >= _bytes.length) { flushBuffer(); } } else { // Writes more than one byte. write2(code); } }
public static RestResponse response(RestChannel channel, String text) throws IOException { try (UTF8StreamWriter out = new UTF8StreamWriter(); BytesStreamOutput bytesOut = channel.bytesOutput()) { out.setOutput(bytesOut); out.append(text); return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes()); } }
public static UTF8StreamWriter utf8StreamWriter() { SoftReference<UTF8StreamWriter> ref = utf8StreamWriter.get(); UTF8StreamWriter writer = (ref == null) ? null : ref.get(); if (writer == null) { writer = new UTF8StreamWriter(1024 * 4); utf8StreamWriter.set(new SoftReference<UTF8StreamWriter>(writer)); } writer.reset(); return writer; } }
/** * Flushes the stream. If the stream has saved any characters from the * various write() methods in a buffer, write them immediately to their * intended destination. Then, if that destination is another character or * byte stream, flush it. Thus one flush() invocation will flush all the * buffers in a chain of Writers and OutputStreams. * * @throws IOException if an I/O error occurs. */ @Override public void flush() throws IOException { flushBuffer(); _outputStream.flush(); }
/** * @deprecated Replaced by {@link #setOutput(OutputStream)} */ @Deprecated public UTF8StreamWriter setOutputStream(OutputStream out) { return this.setOutput(out); } }
public static void pad(Table.Cell cell, int width, RestRequest request, UTF8StreamWriter out, boolean isLast) throws IOException { String sValue = renderValue(request, cell.value); int length = sValue == null ? 0 : sValue.length(); byte leftOver = (byte) (width - length); String textAlign = cell.attr.get("text-align"); if (textAlign == null) { textAlign = "left"; } if (leftOver > 0 && textAlign.equals("right")) { for (byte i = 0; i < leftOver; i++) { out.append(" "); } if (sValue != null) { out.append(sValue); } } else { if (sValue != null) { out.append(sValue); } // Ignores the leftover spaces if the cell is the last of the column. if (!isLast) { for (byte i = 0; i < leftOver; i++) { out.append(" "); } } } }
/** * Writes a single character. This method supports 16-bits * character surrogates. * * @param c <code>char</code> the character to be written (possibly * a surrogate). * @throws IOException if an I/O error occurs. */ public void write(char c) throws IOException { if ((c < 0xd800) || (c > 0xdfff)) { write((int) c); } else if (c < 0xdc00) { // High surrogate. _highSurrogate = c; } else { // Low surrogate. int code = ((_highSurrogate - 0xd800) << 10) + (c - 0xdc00) + 0x10000; write(code); } }
public static RestResponse buildTextPlainResponse(Table table, RestChannel channel) throws IOException { RestRequest request = channel.request(); boolean verbose = request.paramAsBoolean("v", false); List<DisplayHeader> headers = buildDisplayHeaders(table, request); int[] width = buildWidths(table, request, verbose, headers); try (BytesStreamOutput bytesOut = channel.bytesOutput(); UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut)) { if (verbose) { for (int col = 0; col < headers.size(); col++) { DisplayHeader header = headers.get(col); pad(new Table.Cell(header.display, table.findHeaderByName(header.name)), width[col], request, out); out.append("\t\t"); } out.append("\n"); } for (int row = 0; row < table.getRows().size(); row++) { for (int col = 0; col < headers.size(); col++) { DisplayHeader header = headers.get(col); pad(table.getAsMap().get(header.name).get(row), width[col], request, out); out.append("\t\t"); } out.append("\n"); } return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes()); } }
/** * Writes a character given its 31-bits Unicode. * * @param code the 31 bits Unicode of the character to be written. * @throws IOException if an I/O error occurs. */ @Override public void write(int code) throws IOException { if ((code & 0xffffff80) == 0) { _bytes[_index] = (byte) code; if (++_index >= _bytes.length) { flushBuffer(); } } else { // Writes more than one byte. write2(code); } }
/** If exists, reset and return, otherwise create, reset and return a writer.*/ private static UTF8StreamWriter utf8StreamWriter() { SoftReference<UTF8StreamWriter> ref = utf8StreamWriter.get(); UTF8StreamWriter writer = (ref == null) ? null : ref.get(); if (writer == null) { writer = new UTF8StreamWriter(1024 * 4); utf8StreamWriter.set(new SoftReference<>(writer)); } writer.reset(); return writer; }
_bytes[_index] = (byte) (0xc0 | (c >> 6)); if (++_index >= _bytes.length) { flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer(); flushBuffer();
/** * @deprecated Replaced by {@link #setOutput(OutputStream)} */ @Deprecated public UTF8StreamWriter setOutputStream(OutputStream out) { return this.setOutput(out); } }
public static void pad(Table.Cell cell, int width, RestRequest request, UTF8StreamWriter out) throws IOException { String sValue = renderValue(request, cell.value); int length = sValue == null ? 0 : sValue.length(); byte leftOver = (byte) (width - length); String textAlign = cell.attr.get("text-align"); if (textAlign == null) { textAlign = "left"; } if (leftOver > 0 && textAlign.equals("right")) { for (byte i = 0; i < leftOver; i++) { out.append(" "); } if (sValue != null) { out.append(sValue); } } else { if (sValue != null) { out.append(sValue); } for (byte i = 0; i < leftOver; i++) { out.append(" "); } } }
/** * Writes a single character. This method supports 16-bits * character surrogates. * * @param c <code>char</code> the character to be written (possibly * a surrogate). * @throws IOException if an I/O error occurs. */ public void write(char c) throws IOException { if ((c < 0xd800) || (c > 0xdfff)) { write((int) c); } else if (c < 0xdc00) { // High surrogate. _highSurrogate = c; } else { // Low surrogate. int code = ((_highSurrogate - 0xd800) << 10) + (c - 0xdc00) + 0x10000; write(code); } }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { boolean helpWanted = request.paramAsBoolean("help", false); if (helpWanted) { return channel -> { Table table = getTableWithHeader(request); int[] width = buildHelpWidths(table, request); BytesStream bytesOutput = Streams.flushOnCloseStream(channel.bytesOutput()); UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOutput); for (Table.Cell cell : table.getHeaders()) { // need to do left-align always, so create new cells pad(new Table.Cell(cell.value), width[0], request, out); out.append(" | "); pad(new Table.Cell(cell.attr.containsKey("alias") ? cell.attr.get("alias") : ""), width[1], request, out); out.append(" | "); pad(new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"), width[2], request, out); out.append("\n"); } out.close(); channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOutput.bytes())); }; } else { return doCatRequest(request, client); } }
/** * Writes a portion of a string. * * @param str a String. * @param off the offset from which to start writing characters. * @param len the number of characters to write. * @throws IOException if an I/O error occurs */ @Override public void write(String str, int off, int len) throws IOException { final int off_plus_len = off + len; for (int i = off; i < off_plus_len; ) { char c = str.charAt(i++); if (c < 0x80) { _bytes[_index] = (byte) c; if (++_index >= _bytes.length) { flushBuffer(); } } else { write(c); } } }
/** * Closes and {@link #reset resets} this writer for reuse. * * @throws IOException if an I/O error occurs */ @Override public void close() throws IOException { if (_outputStream != null) { flushBuffer(); _outputStream.close(); reset(); } }