Description
Unserialize the given serialized URL-safe string.
License
Apache License
Parameter
Parameter | Description |
---|
string | The serialized URL-safe string to be unserialized. |
Exception
Parameter | Description |
---|
IllegalArgumentException | When the given serialized URL-safe string is not in valid format as returned by#serializeURLSafe(String). |
Return
The unserialized string, or
null
when the given string is by itself
null
.
Declaration
public static String unserializeURLSafe(String string)
Method Source Code
//package com.java2s;
/*//from ww w . ja v a 2s .c om
* Copyright 2013 OmniFaces.
*
* Licensed 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.InflaterInputStream;
import javax.xml.bind.DatatypeConverter;
public class Main {
private static final int DEFAULT_STREAM_BUFFER_SIZE = 10240;
/**
* Unserialize the given serialized URL-safe string. This does the reverse of {@link #serializeURLSafe(String)}.
* @param string The serialized URL-safe string to be unserialized.
* @return The unserialized string, or <code>null</code> when the given string is by itself <code>null</code>.
* @throws IllegalArgumentException When the given serialized URL-safe string is not in valid format as returned by
* {@link #serializeURLSafe(String)}.
* @since 1.2
*/
public static String unserializeURLSafe(String string) {
if (string == null) {
return null;
}
try {
String base64 = string.replace('~', '/').replace('-', '+')
.replace('_', '=');
InputStream deflated = new ByteArrayInputStream(
DatatypeConverter.parseBase64Binary(base64));
ByteArrayOutputStream raw = new ByteArrayOutputStream();
stream(new InflaterInputStream(deflated), raw);
return new String(raw.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
// This will occur when UTF-8 is not supported, but this is not to be expected these days.
throw new RuntimeException(e);
} catch (Exception e) {
// This will occur when the string is not in valid Base64 or ZLIB format.
throw new IllegalArgumentException(e);
}
}
/**
* Stream the given input to the given output by NIO {@link ByteBuffer}. Both the input and output streams will
* implicitly be closed after streaming, regardless of whether an exception is been thrown or not.
* @param input The input stream.
* @param output The output stream.
* @return The length of the written bytes.
* @throws IOException When an I/O error occurs.
*/
public static long stream(InputStream input, OutputStream output)
throws IOException {
ReadableByteChannel inputChannel = null;
WritableByteChannel outputChannel = null;
try {
inputChannel = Channels.newChannel(input);
outputChannel = Channels.newChannel(output);
ByteBuffer buffer = ByteBuffer
.allocateDirect(DEFAULT_STREAM_BUFFER_SIZE);
long size = 0;
while (inputChannel.read(buffer) != -1) {
buffer.flip();
size += outputChannel.write(buffer);
buffer.clear();
}
return size;
} finally {
close(outputChannel);
close(inputChannel);
}
}
/**
* Check if the given resource is not <code>null</code> and then close it, whereby any caught {@link IOException}
* is been returned instead of thrown, so that the caller can if necessary handle (log) or just ignore it without
* the need to put another try-catch.
* @param resource The closeable resource to be closed.
* @return The caught {@link IOException}, or <code>null</code> if none is been thrown.
*/
public static IOException close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
return e;
}
}
return null;
}
}
Related
- toRoboCommand(int command)
- toString(byte[] edid)
- toString(final Serializable o)
- toString(Serializable o)
- toTime(String st)
- writeKey(Writer writer, PublicKey publicKey)
- writeOpaqueValue(final String opaque)