001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.swaps; 018 019import static org.apache.juneau.common.utils.StringUtils.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.swap.*; 023 024/** 025 * Transforms <code><jk>byte</jk>[]</code> arrays to Strings. 026 * 027 * <h5 class='section'>See Also:</h5><ul> 028 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SwapBasics">Swap Basics</a> 029 030 * </ul> 031 */ 032public abstract class ByteArraySwap extends StringSwap<byte[]> { 033 034 /** 035 * Converts byte arrays to BASE-64 encoding. 036 */ 037 public static class Base64 extends ByteArraySwap { 038 /** 039 * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}. 040 */ 041 @Override /* ObjectSwap */ 042 public String swap(BeanSession session, byte[] b) throws Exception { 043 return base64Encode(b); 044 } 045 046 /** 047 * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>. 048 */ 049 @Override /* ObjectSwap */ 050 public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception { 051 return base64Decode(s); 052 } 053 } 054 055 /** 056 * Converts byte arrays to hex encoding. 057 */ 058 public static class Hex extends ByteArraySwap { 059 /** 060 * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}. 061 */ 062 @Override /* ObjectSwap */ 063 public String swap(BeanSession session, byte[] b) throws Exception { 064 return toHex(b); 065 } 066 067 /** 068 * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>. 069 */ 070 @Override /* ObjectSwap */ 071 public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception { 072 return fromHex(s); 073 } 074 } 075 076 /** 077 * Converts byte arrays to spaced-hex encoding. 078 */ 079 public static class SpacedHex extends ByteArraySwap { 080 /** 081 * Converts the specified <code><jk>byte</jk>[]</code> to a {@link String}. 082 */ 083 @Override /* ObjectSwap */ 084 public String swap(BeanSession session, byte[] b) throws Exception { 085 return toSpacedHex(b); 086 } 087 088 /** 089 * Converts the specified {@link String} to a <code><jk>byte</jk>[]</code>. 090 */ 091 @Override /* ObjectSwap */ 092 public byte[] unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception { 093 return fromSpacedHex(s); 094 } 095 } 096}