| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.io; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | |
| 26 | import org.jclouds.encryption.internal.Base64; |
| 27 | |
| 28 | import com.google.common.annotations.Beta; |
| 29 | import com.google.common.annotations.VisibleForTesting; |
| 30 | import com.google.common.base.Charsets; |
| 31 | import com.google.common.io.ByteStreams; |
| 32 | import com.google.common.io.InputSupplier; |
| 33 | |
| 34 | /** |
| 35 | * functions related to or replacing those in {@link com.google.common.io.InputSupplier} |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | @Beta |
| 40 | public class InputSuppliers { |
| 41 | /** |
| 42 | * base64 encodes bytes from the supplied supplier as they are read. |
| 43 | */ |
| 44 | public static Base64InputSupplier base64Encoder(InputSupplier<? extends InputStream> supplier) throws IOException { |
| 45 | return new Base64InputSupplier(supplier, Base64.ENCODE + Base64.DONT_BREAK_LINES); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * base64 decodes bytes from the supplied supplier as they are read. |
| 50 | */ |
| 51 | public static Base64InputSupplier base64Decoder(InputSupplier<? extends InputStream> supplier) throws IOException { |
| 52 | return new Base64InputSupplier(supplier, Base64.DECODE); |
| 53 | } |
| 54 | |
| 55 | @VisibleForTesting |
| 56 | static class Base64InputSupplier implements InputSupplier<InputStream> { |
| 57 | |
| 58 | private final InputSupplier<? extends InputStream> delegate; |
| 59 | private final int mode; |
| 60 | |
| 61 | Base64InputSupplier(InputSupplier<? extends InputStream> inputSupplier, int mode) { |
| 62 | this.delegate = checkNotNull(inputSupplier, "delegate"); |
| 63 | this.mode = mode; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public InputStream getInput() throws IOException { |
| 68 | return new Base64.InputStream(delegate.getInput(), mode); |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | public static InputSupplier<? extends InputStream> of(final InputStream in) { |
| 74 | checkNotNull(in, "in"); |
| 75 | return new InputSupplier<InputStream>() { |
| 76 | |
| 77 | @Override |
| 78 | public InputStream getInput() throws IOException { |
| 79 | return in; |
| 80 | } |
| 81 | |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | public static InputSupplier<? extends InputStream> of(byte[] in) { |
| 86 | return ByteStreams.newInputStreamSupplier(checkNotNull(in, "in")); |
| 87 | } |
| 88 | |
| 89 | public static InputSupplier<? extends InputStream> of(String in) { |
| 90 | return of(checkNotNull(in, "in").getBytes(Charsets.UTF_8)); |
| 91 | } |
| 92 | } |