Java String Decode decodeString(StringReader in)

Here you can find the source of decodeString(StringReader in)

Description

Decode a string previously encoded through encodeString.

License

Apache License

Parameter

Parameter Description
in The input StringReader.

Exception

Parameter Description
IOException an exception

Return

The decoded string object.

Declaration

public static String decodeString(StringReader in) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w w w  .  j a  v a 2s  . c o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.IOException;
import java.io.StringReader;

public class Main {
    /**
     * Decode a string previously encoded through encodeString.
     * 
     * @param in
     *          The input StringReader.
     * @return The decoded string object.
     * @throws IOException
     */
    public static String decodeString(StringReader in) throws IOException {
        Integer len = decodeInt(in);
        if (len == null) {
            return null;
        }

        char[] chars = new char[len];
        in.read(chars);
        return new String(chars);
    }

    public static Integer decodeInt(StringReader in) throws IOException {
        Long l = decodeLong(in);
        if (l == null)
            return null;
        return (int) l.longValue();
    }

    public static Long decodeLong(StringReader in) throws IOException {
        int c = in.read();
        if (c == ':') {
            return null;
        }
        if (Character.isDigit(c) == false) {
            throw new IllegalArgumentException("Bad encoded string");
        }
        StringBuilder sb = new StringBuilder();
        sb.append((char) c);

        boolean colonSeen = false;
        while ((c = in.read()) != -1) {
            if (c == ':') {
                colonSeen = true;
                break;
            }
            if (!Character.isDigit(c)) {
                break;
            }
            sb.append((char) c);
        }
        if (colonSeen == false) {
            throw new IllegalArgumentException("Bad encoded string");
        }
        return new Long(sb.toString());
    }
}

Related

  1. decodeString(String s)
  2. decodeString(String s, String charset)
  3. decodeString(String s, String encoding)
  4. decodeString(String str)
  5. decodeString(String str)
  6. decodeStringFromByteArray(byte[] data)
  7. decodeStrings(byte[] stringBytes)
  8. decodeText(String encodeText)
  9. decodeText(String s)