Example usage for java.lang StringBuffer getChars

List of usage examples for java.lang StringBuffer getChars

Introduction

In this page you can find the example usage for java.lang StringBuffer getChars.

Prototype

@Override
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 

Source Link

Usage

From source file:Main.java

public static void main(String args[]) {
    StringBuffer buffer = new StringBuffer("hello there from java2s.com");

    char charArray[] = new char[buffer.length()];
    buffer.getChars(0, buffer.length(), charArray, 0);
    System.out.print("The characters are: ");

}

From source file:MainClass.java

public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");

    System.out.println(phrase.charAt(5));

    char[] textArray = new char[3];
    phrase.getChars(9, 12, textArray, 0);

    for (char ch : textArray) {
        System.out.println(ch);//from w ww.  ja v  a 2s .c  om

    }
}

From source file:Main.java

public static void main(String args[]) {
    StringBuffer buffer = new StringBuffer("hello there");

    System.out.printf("buffer = %s\n", buffer.toString());
    System.out.printf("Character at 0: %s\nCharacter at 4: %s\n\n", buffer.charAt(0), buffer.charAt(4));

    char charArray[] = new char[buffer.length()];
    buffer.getChars(0, buffer.length(), charArray, 0);
    System.out.print("The characters are: ");

}

From source file:MainClass.java

public static void main(String args[]) {
    StringBuffer buffer = new StringBuffer("hello there");

    System.out.printf("buffer = %s\n", buffer.toString());
    System.out.printf("Character at 0: %s\nCharacter at 4: %s\n\n", buffer.charAt(0), buffer.charAt(4));

    char charArray[] = new char[buffer.length()];
    buffer.getChars(0, buffer.length(), charArray, 0);
    System.out.print("The characters are: ");

    for (char character : charArray)
        System.out.print(character);

    buffer.setCharAt(0, 'H');
    buffer.setCharAt(6, 'T');
    System.out.printf("\n\nbuf = %s", buffer.toString());

    buffer.reverse();//from  w w  w .ja v a2s . c o m
    System.out.printf("\n\nbuf = %s\n", buffer.toString());
}

From source file:TextVerifyInputFormatDate.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.BORDER);
    text.setText("YYYY/MM/DD");
    ;/* ww w  .j a  v a 2 s .c  o m*/
    final Calendar calendar = Calendar.getInstance();
    text.addListener(SWT.Verify, new Listener() {
        boolean ignore;

        public void handleEvent(Event e) {
            if (ignore)
                return;
            e.doit = false;
            StringBuffer buffer = new StringBuffer(e.text);
            char[] chars = new char[buffer.length()];
            buffer.getChars(0, chars.length, chars, 0);
            if (e.character == '\b') {
                for (int i = e.start; i < e.end; i++) {
                    switch (i) {
                    case 0: /* [Y]YYY */
                    case 1: /* Y[Y]YY */
                    case 2: /* YY[Y]Y */
                    case 3: /* YYY[Y] */ {
                        buffer.append('Y');
                        break;
                    }
                    case 5: /* [M]M */
                    case 6: /* M[M] */ {
                        buffer.append('M');
                        break;
                    }
                    case 8: /* [D]D */
                    case 9: /* D[D] */ {
                        buffer.append('D');
                        break;
                    }
                    case 4: /* YYYY[/]MM */
                    case 7: /* MM[/]DD */ {
                        buffer.append('/');
                        break;
                    }
                    default:
                        return;
                    }
                }
                text.setSelection(e.start, e.start + buffer.length());
                ignore = true;
                text.insert(buffer.toString());
                ignore = false;
                text.setSelection(e.start, e.start);
                return;
            }

            int start = e.start;
            if (start > 9)
                return;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                if (start + index == 4 || start + index == 7) {
                    if (chars[i] == '/') {
                        index++;
                        continue;
                    }
                    buffer.insert(index++, '/');
                }
                if (chars[i] < '0' || '9' < chars[i])
                    return;
                if (start + index == 5 && '1' < chars[i])
                    return; /* [M]M */
                if (start + index == 8 && '3' < chars[i])
                    return; /* [D]D */
                index++;
            }
            String newText = buffer.toString();
            int length = newText.length();
            StringBuffer date = new StringBuffer(text.getText());
            date.replace(e.start, e.start + length, newText);
            calendar.set(Calendar.YEAR, 1901);
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DATE, 1);
            String yyyy = date.substring(0, 4);
            if (yyyy.indexOf('Y') == -1) {
                int year = Integer.parseInt(yyyy);
                calendar.set(Calendar.YEAR, year);
            }
            String mm = date.substring(5, 7);
            if (mm.indexOf('M') == -1) {
                int month = Integer.parseInt(mm) - 1;
                int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
                if (0 > month || month > maxMonth)
                    return;
                calendar.set(Calendar.MONTH, month);
            }
            String dd = date.substring(8, 10);
            if (dd.indexOf('D') == -1) {
                int day = Integer.parseInt(dd);
                int maxDay = calendar.getActualMaximum(Calendar.DATE);
                if (1 > day || day > maxDay)
                    return;
                calendar.set(Calendar.DATE, day);
            } else {
                if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) {
                    char firstChar = date.charAt(8);
                    if (firstChar != 'D' && '2' < firstChar)
                        return;
                }
            }
            text.setSelection(e.start, e.start + length);
            ignore = true;
            text.insert(newText);
            ignore = false;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

/**
 * Replaces HTML entities in a string buffer
 * @param buffer the string buffer//from   w  w w  . j  a  v a2s.c o m
 */
public static void replaceEntities(StringBuffer buffer) {
    int i = 0;
    while (i < buffer.length()) {
        if (buffer.charAt(i) == '&') {
            int j = i + 1;
            while (j < buffer.length() && buffer.charAt(j) != ';')
                j++;
            if (j < buffer.length()) {
                char[] chars = new char[j - i - 1];
                buffer.getChars(i + 1, j, chars, 0);
                Character repl = (Character) replacements.get(new String(chars));
                if (repl != null) {
                    buffer.delete(i, j);
                    buffer.setCharAt(i, repl.charValue());
                } else
                    i = j;
            } else
                i = j;
        }
        i++;
    }
}

From source file:com.ciphertool.zodiacengine.util.RandomWordSolutionGenerator.java

public void convertWordsToPlaintext(SolutionChromosome solution, List<Word> wordList) {
    StringBuffer rawText = new StringBuffer();
    for (Word w : wordList) {
        rawText.append(w.getId().getWord());
    }/*  w  ww  . j  av  a  2  s. co  m*/
    char[] chars = new char[cipher.length()];
    rawText.getChars(0, cipher.length(), chars, 0);
    int id = 1;
    Plaintext pt;
    for (char c : chars) {
        pt = new Plaintext(id, String.valueOf(c));
        solution.addPlaintext(pt);
        id++;
    }
}

From source file:wicket.protocol.http.WebResponse.java

/**
 * Writes string to response output./*www.j  ava 2  s  .  com*/
 * 
 * @param string
 *            The string to write
 */
@Override
public void write(final CharSequence string) {
    if (string instanceof AppendingStringBuffer) {
        write((AppendingStringBuffer) string);
    } else if (string instanceof StringBuffer) {
        try {
            StringBuffer sb = (StringBuffer) string;
            char[] array = new char[sb.length()];
            sb.getChars(0, sb.length(), array, 0);
            httpServletResponse.getWriter().write(array, 0, array.length);
        } catch (IOException e) {
            throw new WicketRuntimeException("Error while writing to servlet output writer.", e);
        }
    } else {
        try {
            httpServletResponse.getWriter().write(string.toString());
        } catch (IOException e) {
            throw new WicketRuntimeException("Error while writing to servlet output writer.", e);
        }
    }
}

From source file:com.trafficspaces.api.controller.Connector.java

private char[] readResponseData(InputStream stream, String encoding) {
    BufferedReader in = null;//ww w  . ja v  a  2 s. co m
    char[] data = null;
    try {
        StringBuffer buf = new StringBuffer();
        data = new char[1024];

        in = new BufferedReader(new InputStreamReader(stream, encoding));
        int charsRead;
        while ((charsRead = in.read(data)) != -1) {
            buf.append(data, 0, charsRead);
        }
        data = new char[buf.length()];
        buf.getChars(0, data.length, data, 0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return data != null ? data : null;
}

From source file:org.apache.cocoon.template.instruction.Comment.java

public Event execute(final XMLConsumer consumer, ExpressionContext expressionContext,
        ExecutionContext executionContext, MacroContext macroContext, Event startEvent, Event endEvent)
        throws SAXException {
    // Parse the body of the comment
    NodeList nodeList = Invoker.toDOMNodeList("comment", this, expressionContext, executionContext,
            macroContext);//  ww  w.j  av a  2 s .  c om
    // JXPath doesn't handle NodeList, so convert it to an array
    int len = nodeList.getLength();
    final StringBuffer buf = new StringBuffer();
    Properties omit = XMLUtils.createPropertiesForXML(true);
    for (int i = 0; i < len; i++) {
        try {
            String str = XMLUtils.serializeNode(nodeList.item(i), omit);
            buf.append(StringUtils.substringAfter(str, ">")); // cut
            // the XML header
        } catch (ProcessingException e) {
            throw new SAXParseException(e.getMessage(), getLocation(), e);
        }
    }
    char[] chars = new char[buf.length()];
    buf.getChars(0, chars.length, chars, 0);
    consumer.comment(chars, 0, chars.length);
    return getEndInstruction().getNext();
}