001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.transforms;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.io.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.transform.*;
022
023/**
024 * Transforms <code>InputStreams</code> to {@link String Strings}.
025 */
026public abstract class InputStreamSwap extends StringSwap<InputStream> {
027
028   /**
029    * Converts {@link InputStream InputStreams} to BASE-64 encoding.
030    */
031   public static class Base64 extends InputStreamSwap {
032      /**
033       * Converts the specified {@link InputStream} to a {@link String}.
034       */
035      @Override /* PojoSwap */
036      public String swap(BeanSession session, InputStream is) throws Exception {
037         return base64Encode(toBytes(is));
038      }
039
040      /**
041       * Converts the specified {@link String} to an {@link InputStream}.
042       */
043      @Override /* PojoSwap */
044      public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
045         return toStream(base64Decode(s), hint);
046      }
047   }
048
049   /**
050    * Converts {@link InputStream InputStreams} to hex encoding.
051    */
052   public static class Hex extends InputStreamSwap {
053      /**
054       * Converts the specified {@link InputStream} to a {@link String}.
055       */
056      @Override /* PojoSwap */
057      public String swap(BeanSession session, InputStream is) throws Exception {
058         return toHex(toBytes(is));
059      }
060
061      /**
062       * Converts the specified {@link String} to an {@link InputStream}.
063       */
064      @Override /* PojoSwap */
065      public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
066         return toStream(fromHex(s), hint);
067      }
068   }
069
070   /**
071    * Converts {@link InputStream InputStreams} to spaced-hex encoding.
072    */
073   public static class SpacedHex extends InputStreamSwap {
074      /**
075       * Converts the specified {@link InputStream} to a {@link String}.
076       */
077      @Override /* PojoSwap */
078      public String swap(BeanSession session, InputStream is) throws Exception {
079         return toSpacedHex(toBytes(is));
080      }
081
082      /**
083       * Converts the specified {@link String} to an {@link InputStream}.
084       */
085      @Override /* PojoSwap */
086      public InputStream unswap(BeanSession session, String s, ClassMeta<?> hint) throws Exception {
087         return toStream(fromSpacedHex(s), hint);
088      }
089   }
090
091   /**
092    * Convert the specified input stream to a byte array.
093    *
094    * @param is The input stream to convert to bytes.
095    * @return The byte array.
096    * @throws IOException Thrown by input stream.
097    */
098   protected byte[] toBytes(InputStream is) throws IOException {
099      return IOUtils.readBytes(is);
100   }
101
102
103   /**
104    * Convert the specified byte array into an input stream.
105    *
106    * @param b The byte array.
107    * @param hint Contains a hint about what subtype is being requested.
108    * @return The byte array.
109    */
110   protected InputStream toStream(byte[] b, ClassMeta<?> hint) {
111      Class<?> c = hint == null ? InputStream.class : hint.getInnerClass();
112      if (c == InputStream.class || c == ByteArrayInputStream.class)
113         return new ByteArrayInputStream(b);
114      return null;
115   }
116}