Example usage for java.lang String getChars

List of usage examples for java.lang String getChars

Introduction

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

Prototype

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

Source Link

Document

Copies characters from this string into the destination character array.

Usage

From source file:uk.ac.gda.dls.client.views.ScannableCompositeFactory.java

private void verify(VerifyEvent e) {
    String string = e.text;
    char[] chars = new char[string.length()];
    string.getChars(0, chars.length, chars, 0);

    Text text = (Text) e.getSource();

    if ((",".equals(string) || ".".equals(string)) && text.getText().indexOf(',') >= 0) {
        e.doit = false;// ww w.  j  a v  a 2s .co m
        return;
    }

    for (int i = 0; i < chars.length; i++) {
        if (!(('0' <= chars[i] && chars[i] <= '9') || chars[i] == '.' || chars[i] == ',' || chars[i] == '-')) {
            e.doit = false;
            return;
        }

        if (chars[i] == '.') {
            chars[i] = ',';
        }
    }

    e.text = new String(chars);

    final String oldS = text.getText();
    String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
    Matcher matcher = pattern.matcher(newS);
    if (!matcher.matches()) {
        e.doit = false;
        return;
    }

}

From source file:org.pentaho.pms.ui.concept.editor.ColumnWidthPropertyEditorWidget.java

protected void createContents(final Composite parent) {
    addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            ColumnWidthPropertyEditorWidget.this.widgetDisposed(e);
        }/*from  w ww. ja v  a  2 s.  c  o m*/
    });
    typeLabel = new Label(parent, SWT.NONE);
    typeLabel.setText("Column Width Type:"); //$NON-NLS-1$

    type = new Combo(parent, SWT.READ_ONLY | SWT.BORDER);

    typeComboViewer = new ComboViewer(type);

    typeComboViewer.setContentProvider(new IStructuredContentProvider() {
        public Object[] getElements(final Object inputElement) {
            return (ColumnWidth[]) inputElement;
        }

        public void dispose() {
        }

        public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
        }
    });

    typeComboViewer.setInput(ColumnWidth.types);

    typeComboViewer.setLabelProvider(new LabelProvider() {
        public Image getImage(final Object element) {
            // no images in this combo
            return null;
        }

        public String getText(final Object element) {
            return ((ColumnWidth) element).getDescription();
        }
    });

    FormData fdType = new FormData();
    fdType.left = new FormAttachment(typeLabel, 10);
    fdType.top = new FormAttachment(0, 0);
    type.setLayoutData(fdType);

    FormData fdTypeLabel = new FormData();
    fdTypeLabel.left = new FormAttachment(0, 0);
    fdTypeLabel.top = new FormAttachment(type, 0, SWT.CENTER);
    typeLabel.setLayoutData(fdTypeLabel);

    widthLabel = new Label(parent, SWT.NONE);
    widthLabel.setText("Column Width:"); //$NON-NLS-1$

    width = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.LEFT);

    // only allow digits
    width.addListener(SWT.Verify, new Listener() {
        public void handleEvent(final Event e) {
            String string = e.text;
            char[] chars = new char[string.length()];
            string.getChars(0, chars.length, chars, 0);
            for (int i = 0; i < chars.length; i++) {
                if (!(Character.isDigit(chars[i]))) {
                    e.doit = false;
                    return;
                }
            }
        }
    });

    //    width.setToolTipText(Messages.getString("ConceptPropertyColumnWidthWidget.USER_SELECT_PROPERTY_WIDTH", name)); //$NON-NLS-1$
    FormData fdWidth = new FormData();
    fdWidth.left = new FormAttachment(widthLabel, 10);
    fdWidth.right = new FormAttachment(100, 0);
    fdWidth.top = new FormAttachment(type, 10);
    width.setLayoutData(fdWidth);

    FormData fdWidthLabel = new FormData();
    fdWidthLabel.left = new FormAttachment(0, 0);
    fdWidthLabel.top = new FormAttachment(width, 0, SWT.CENTER);
    widthLabel.setLayoutData(fdWidthLabel);

    typeComboViewer.addSelectionChangedListener(this);
    width.addFocusListener(this);
}

From source file:com.jcraft.weirdx.res.XFont.java

public static void reqQueryTextExtents(Client c) throws IOException {
    int n, foo;/*from  w  ww . ja va  2 s.c o m*/
    boolean odd = false;
    InputOutput io = c.client;
    foo = c.data;
    if (foo != 0)
        odd = true;
    n = c.length;
    foo = io.readInt();
    c.length -= 2;
    XFont f = (XFont) XResource.lookupIDByType(foo, RT_FONT);
    if (f == null) {
        c.errorValue = foo;
        c.errorReason = 7; // BadFont;
        return;
    }
    n = n - 2;
    n *= 4;
    io.readByte(c.bbuffer, 0, n);

    if (odd)
        n -= 2;

    synchronized (io) {
        io.writeByte(1);
        if (f.encoding != null) {
            for (int i = 0; i < n; i++) {
                c.bbuffer[i] |= 0x80;
            }
            try {
                String s = new String(c.bbuffer, 0, n, f.encoding);
                n = s.length();
                s.getChars(0, n, c.cbuffer, 0);
            } catch (Exception e) {
                LOG.error(e);
                return;
            }
        } else {
            for (int i = 0; i < n; i++) {
                c.cbuffer[i] = (char) c.bbuffer[i];
            }
        }

        foo = f.charsWidth(c.cbuffer, 0, n);

        io.writeByte((byte) 0);
        io.writeShort(c.seq);
        io.writeInt(0);
        io.writeShort(f.font.getMaxAscent());
        io.writeShort(f.font.getMaxDescent());
        io.writeShort(f.font.getMaxAscent());
        io.writeShort(f.font.getMaxDescent());
        io.writeInt(foo);
        io.writeInt(0);
        io.writeInt(foo);
        io.writePad(4);
        io.flush();
    }
}

From source file:de.hybris.platform.cuppytrail.impl.DefaultSecureTokenService.java

private byte[] decodeHexString(final String text) throws DecoderException {
    final char[] chars = new char[text.length()];
    text.getChars(0, text.length(), chars, 0);
    return Hex.decodeHex(chars);
}

From source file:com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable.java

/**
 * {@inheritDoc}/*from   ww  w .  j  a  va  2 s.  c om*/
 * Same as base implementation, but includes all methods inherited from super classes as well.
 */
@Override
public void defineProperty(final String propertyName, final Class<?> clazz, int attributes) {
    final int length = propertyName.length();
    if (length == 0) {
        throw new IllegalArgumentException();
    }
    final char[] buf = new char[3 + length];
    propertyName.getChars(0, length, buf, 3);
    buf[3] = Character.toUpperCase(buf[3]);
    buf[0] = 'g';
    buf[1] = 'e';
    buf[2] = 't';
    final String getterName = new String(buf);
    buf[0] = 's';
    final String setterName = new String(buf);

    final Method[] methods = clazz.getMethods();
    final Method getter = findMethod(methods, getterName);
    final Method setter = findMethod(methods, setterName);
    if (setter == null) {
        attributes |= ScriptableObject.READONLY;
    }
    defineProperty(propertyName, null, getter, setter, attributes);
}

From source file:xbird.xquery.misc.BasicStringChunk.java

public long store(final String s) {
    assert (s != null);
    final int strlen = s.length();
    if (strlen < CHUNKED_THRESHOLD) {
        s.getChars(0, strlen, tmpBuf, 0);
        return store(tmpBuf, 0, strlen);
    }//from www .  j  ava 2 s.  com
    return allocateStringChunk(s);
}

From source file:org.openhab.binding.echonetlite.internal.ECHONETLiteConnectingThread.java

/**
 * Receives ECHONETLite reply message Checks if it is an error message
 *//*from ww w. j  a v a2 s.  co m*/
char[] receiveMessage() {
    logger.debug("checking the response from the ECHONETLite device");
    char[] res = { '0', '0', '0', '0' };

    try {
        String response = udpConnector.receiveDatagram(host);
        if (response.equals("nothing")) {
            logger.debug("Nothing was sent back from ECHONETLite devices");
        } else {
            char[] code = new char[response.length()];
            response.getChars(0, response.length(), code, 0);

            if (response.length() > 21) {
                if (response.length() > 22 && code[20] == '5') {
                    logger.info("An error message (ESV=" + code[20] + code[21]
                            + ") was sent back by the ECHONETLite device, the device's state was not updated: please check the message sent previously");
                } else if (code[20] == '7') {
                    if (code[21] == '1') {
                        logger.info("SetC was successfully executed by the ECHONETLite device");
                        res = ArrayUtils.subarray(code, 26, 28);
                    } else if (code[21] == '2') {
                        logger.info("Get was successfully executed by the ECHONETLite device");
                        int resSize = Integer.parseInt(String.valueOf(ArrayUtils.subarray(code, 26, 28)), 16);
                        int resFrom = 28;
                        int resTo = 28 + resSize * 2;
                        res = ArrayUtils.subarray(code, resFrom, resTo);
                    } else {
                        logger.info("receive unknown response");
                    }
                }
            }
        }
        return res;
    } catch (Exception e) {
        logger.error(e.toString());
        logger.error("error when receiving message from ECHONETLite devices");
        return res;
    }
}

From source file:net.jawr.web.resource.bundle.factory.util.PathNormalizer.java

/**
 * Internal method to perform the normalization.
 * //from   ww  w.ja  v  a2 s.com
 * @param filename the filename
 * @param keepSeparator true to keep the final separator
 * @return the normalized filename
 */
private static String doNormalizeIgnoreOtherSeparator(String filename, boolean keepSeparator) {
    if (filename == null) {
        return null;
    }
    int size = filename.length();
    if (size == 0) {
        return filename;
    }
    int prefix = 0;
    // int prefix = getPrefixLength(filename);
    // if (prefix < 0) {
    // return null;
    // }

    char[] array = new char[size + 2]; // +1 for possible extra slash, +2
    // for arraycopy
    filename.getChars(0, filename.length(), array, 0);

    // add extra separator on the end to simplify code below
    boolean lastIsDirectory = true;
    if (array[size - 1] != JawrConstant.URL_SEPARATOR_CHAR) {
        array[size++] = JawrConstant.URL_SEPARATOR_CHAR;
        lastIsDirectory = false;
    }

    // adjoining slashes
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == JawrConstant.URL_SEPARATOR_CHAR && array[i - 1] == JawrConstant.URL_SEPARATOR_CHAR) {
            System.arraycopy(array, i, array, i - 1, size - i);
            size--;
            i--;
        }
    }

    // dot slash
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == JawrConstant.URL_SEPARATOR_CHAR && array[i - 1] == '.'
                && (i == prefix + 1 || array[i - 2] == JawrConstant.URL_SEPARATOR_CHAR)) {
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            System.arraycopy(array, i + 1, array, i - 1, size - i);
            size -= 2;
            i--;
        }
    }

    // double dot slash
    outer: for (int i = prefix + 2; i < size; i++) {
        if (array[i] == JawrConstant.URL_SEPARATOR_CHAR && array[i - 1] == '.' && array[i - 2] == '.'
                && (i == prefix + 2 || array[i - 3] == JawrConstant.URL_SEPARATOR_CHAR)) {
            if (i == prefix + 2) {
                return null;
            }
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            int j;
            for (j = i - 4; j >= prefix; j--) {
                if (array[j] == JawrConstant.URL_SEPARATOR_CHAR) {
                    // remove b/../ from a/b/../c
                    System.arraycopy(array, i + 1, array, j + 1, size - i);
                    size -= (i - j);
                    i = j + 1;
                    continue outer;
                }
            }
            // remove a/../ from a/../c
            System.arraycopy(array, i + 1, array, prefix, size - i);
            size -= (i + 1 - prefix);
            i = prefix + 1;
        }
    }

    if (size <= 0) { // should never be less than 0
        return "";
    }
    if (size <= prefix) { // should never be less than prefix
        return new String(array, 0, size);
    }
    if (lastIsDirectory && keepSeparator) {
        return new String(array, 0, size); // keep trailing separator
    }
    return new String(array, 0, size - 1); // lose trailing separator
}

From source file:fr.limsi.ARViewer.CharArrayBuffer.java

public void append(String str) {
    if (str == null) {
        str = "null";
    }/* w  w  w .ja va2s.  c  o m*/
    int strlen = str.length();
    int newlen = this.len + strlen;
    if (newlen > this.buffer.length) {
        expand(newlen);
    }
    str.getChars(0, strlen, this.buffer, this.len);
    this.len = newlen;
}

From source file:com.boylesoftware.web.impl.auth.CipherToolbox.java

/**
 * Decrypt specified Base64 value. The decrypted values can be retrieved
 * immediately after calling this method using {@link #getUserId},
 * {@link #getSalt} and {@link #getTimestamp}.
 *
 * @param base64Val The value to decrypt in Base64 encoding.
 *
 * @return {@code true} if decrypted successfully, {@code false} if
 * could not decrypt./*  www . ja  v a  2 s .  c  o m*/
 */
boolean decrypt(final String base64Val) {

    final boolean debug = this.log.isDebugEnabled();
    if (debug)
        this.log.debug("decrypting [" + base64Val + "], pooled cipher " + this);

    try {
        final int len = base64Val.length();
        base64Val.getChars(0, len, this.base64Chars, 0);
        this.base64Buf.limit(len);
        this.base64Buf.rewind();
    } catch (final IndexOutOfBoundsException e) {
        if (debug)
            this.log.debug("decryption error", e);
        return false;
    }

    this.cipherBuf.clear();
    Base64.decode(this.base64Buf, this.cipherBuf);
    this.cipherBuf.flip();

    this.clearBuf.clear();
    try {
        this.cipher.init(Cipher.DECRYPT_MODE, this.secretKey);
        this.cipher.doFinal(this.cipherBuf, this.clearBuf);
    } catch (final GeneralSecurityException e) {
        if (debug)
            this.log.debug("decryption error", e);
        return false;
    }
    this.clearBuf.flip();

    this.salt = this.clearBuf.getInt();
    this.clearBuf.getLong();
    this.userId = this.clearBuf.getInt();
    this.clearBuf.getLong();
    this.timestamp = this.clearBuf.getLong();

    if (debug)
        this.log.debug("decrypted: userId=" + this.userId + ", salt=" + this.salt + ", ts=" + this.timestamp
                + " (" + (new java.util.Date(this.timestamp)) + ")");

    return true;
}