/** * Draw every char in separate terminal cell to guaranty equal width for different lines. * Nevertheless to improve kerning we draw word characters as one block for monospaced fonts. */ private void drawChars(int x, int y, CharBuffer buf, TextStyle style, Graphics2D gfx) { final int blockLen = 1; int offset = 0; int drawCharsOffset = 0; // workaround to fix Swing bad rendering of bold special chars on Linux // TODO required for italic? CharBuffer renderingBuffer; if (mySettingsProvider.DECCompatibilityMode() && style.hasOption(TextStyle.Option.BOLD)) { renderingBuffer = CharUtils.heavyDecCompatibleBuffer(buf); } else { renderingBuffer = buf; } while (offset + blockLen <= buf.length()) { if (renderingBuffer.getBuf()[buf.getStart() + offset] == CharUtils.DWC) { offset += blockLen; drawCharsOffset += blockLen; continue; // dont' draw second part(fake one) of double width character } Font font = getFontToDisplay(buf.charAt(offset + blockLen - 1), style); // while (myMonospaced && (offset + blockLen < buf.getLength()) && // isWordCharacter(buf.charAt(offset + blockLen - 1)) // && (font == getFontToDisplay(buf.charAt(offset + blockLen - 1), style))) { // blockLen++; // } gfx.setFont(font); int descent = gfx.getFontMetrics(font).getDescent(); int baseLine = (y + 1) * myCharSize.height - descent; int xCoord = (x + drawCharsOffset) * myCharSize.width; int textLength = CharUtils.getTextLengthDoubleWidthAware( buf.getBuf(), buf.getStart() + offset, blockLen, mySettingsProvider.ambiguousCharsAreDoubleWidth()); int yCoord = y * myCharSize.height; gfx.setClip( xCoord, yCoord, Math.min(textLength * myCharSize.width, getWidth() - xCoord), Math.min(myCharSize.height, getHeight() - yCoord)); gfx.setColor(getPalette().getColor(myStyleState.getForeground(style.getForegroundForRun()))); gfx.drawChars(renderingBuffer.getBuf(), buf.getStart() + offset, blockLen, xCoord, baseLine); drawCharsOffset += blockLen; offset += blockLen; } gfx.setClip(null); }
private int parseInt(CharBuffer cb) throws Exception { int value = 0; int sign = 1; for (int i = 0; i < cb.length(); i++) { int ch = cb.charAt(i); if (i == 0 && ch == '-') sign = -1; else if (i == 0 && ch == '+') { } else if (ch >= '0' && ch <= '9') value = 10 * value + ch - '0'; else throw new Exception(); } return sign * value; }
/** * Check if the first X characters of a byte stream match a String. * * @param data The byte array to process * @param pattern The String to match * @return True if the pattern was found, false otherwise */ private static boolean bytesEqualsString(byte[] data, String pattern) { byte[] bytes = new byte[pattern.length()]; Charset csets = Charset.forName("US-ASCII"); boolean fin = false; int currChar = 0; // remove any CR and/or LF characters at the beginning of the article // data while (!fin) { if (currChar >= data.length) break; byte in = data[currChar]; ByteBuffer bb = ByteBuffer.wrap(new byte[] {(byte) in}); CharBuffer cb = csets.decode(bb); char c = cb.charAt(0); if (data.length > 0 && (c == '\n' || c == '\r')) currChar++; else fin = true; if (data.length == 0) fin = true; } // extract bytes (chars) to check from article data for (int i = 0; i < bytes.length && i < data.length; i++, currChar++) { byte in = data[currChar]; bytes[i] = (byte) in; } // decode byte data to characters ByteBuffer bb = ByteBuffer.wrap(bytes); CharBuffer cb = csets.decode(bb); // compare these characters to the pattern String for (int i = 0; i < pattern.length(); i++) if (cb.charAt(i) != pattern.charAt(i)) return false; return true; }
/* * XXX: okay, this is vile. * Mon, 17 Jan 1994 11:14:55 -0500 (EST) * * In GMT time */ public long parseDate(String string) throws Exception { try { int strlen = string.length(); if (strlen == 0) return 0; int i = skipWhitespace(string, strlen, 0); int ch = string.charAt(i); if (ch >= '0' && ch <= '9' || (ch == 'T' && i + 1 < strlen && string.charAt(i + 1) >= '0' && string.charAt(i + 1) <= '9')) return parseISO8601Date(string, i); CharBuffer cb = new CharBuffer(); i = scan(string, 0, cb, true); if (cb.length() == 0 || !Character.isDigit(cb.charAt(0))) i = scan(string, i, cb, true); int dayOfMonth = parseInt(cb); i = scan(string, i, cb, true); String smonth = cb.toString(); int month; for (month = 0; month < 12; month++) { if (MONTH_NAMES[(int) month].equalsIgnoreCase(smonth)) break; } if (month == 12) throw new Exception("Unexpected month: " + month); i = scan(string, i, cb, true); int year = parseInt(cb); if (cb.length() < 3 && year < 50) year += 2000; else if (cb.length() < 3 && year < 100) year += 1900; i = scan(string, i, cb, false); long timeOfDay = parseInt(cb) * 3600000; i = scan(string, i, cb, false); timeOfDay += parseInt(cb) * 60000; i = scan(string, i, cb, false); timeOfDay += parseInt(cb) * 1000; // XXX: gross hack if (year <= 1600) dayOfMonth--; long time = (MS_PER_DAY * (yearToDayOfEpoch(year) + monthToDayOfYear(month, isLeapYear(year)) + dayOfMonth - 1) + timeOfDay); try { i = scan(string, i, cb, false); for (int j = 0; j < cb.length(); j++) { if ((ch = cb.charAt(j)) == ';' || ch == ' ') cb.setLength(j); } ch = cb.length() > 0 ? cb.charAt(0) : 0; if (ch == '-' || ch == '+' || ch >= '0' && ch <= '9') { long zoneOffset; zoneOffset = parseInt(cb); zoneOffset = 60000 * (60 * (zoneOffset / 100) + zoneOffset % 100); time -= zoneOffset; setGMTTime(time); } else if (cb.equalsIgnoreCase("gmt") || cb.equalsIgnoreCase("utc")) { setGMTTime(time); } else { setLocalTime(time); } } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } return _localTimeOfEpoch - _zoneOffset; } catch (Exception e) { log.log(Level.FINER, e.toString(), e); return Long.MAX_VALUE; } }
public static void main(String[] args) throws Exception { System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method..."); final String in = "for testing"; System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0."); CharBuffer buff = CharBuffer.wrap(in); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing with new position."); buff.position(2); test(buff, buff.slice()); System.out.println( ">>> StringCharBufferSliceTest-main: testing with non zero initial position."); buff = CharBuffer.wrap(in, 3, in.length()); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get()"); buff.position(4); buff.limit(7); CharBuffer slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get() != buff.get()) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get(int)"); buff.position(4); buff.limit(7); slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get(i) != buff.get(4 + i)) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice with result of slice"); buff.position(0); buff.limit(buff.capacity()); slice = buff.slice(); for (int i = 0; i < 4; i++) { slice.position(i); CharBuffer nextSlice = slice.slice(); if (nextSlice.position() != 0) throw new RuntimeException("New buffer's position should be zero"); if (!nextSlice.equals(slice)) throw new RuntimeException("New buffer should be equal"); slice = nextSlice; } System.out.println(">>> StringCharBufferSliceTest-main: testing toString."); buff.position(4); buff.limit(7); slice = buff.slice(); if (!slice.toString().equals("tes")) { throw new RuntimeException("bad toString() after slice(): " + slice.toString()); } System.out.println(">>> StringCharBufferSliceTest-main: testing subSequence."); buff.position(4); buff.limit(8); slice = buff.slice(); CharSequence subSeq = slice.subSequence(1, 3); if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') { throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: testing duplicate."); buff.position(4); buff.limit(8); slice = buff.slice(); CharBuffer dupe = slice.duplicate(); if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e' || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') { throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: done!"); }