| 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.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.io.File; |
| 25 | import java.io.FileInputStream; |
| 26 | import java.io.FileNotFoundException; |
| 27 | import java.io.InputStream; |
| 28 | |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.io.InputSuppliers; |
| 32 | import org.jclouds.io.Payload; |
| 33 | import org.jclouds.io.PayloadSlicer; |
| 34 | import org.jclouds.io.payloads.BaseMutableContentMetadata; |
| 35 | import org.jclouds.io.payloads.InputStreamSupplierPayload; |
| 36 | |
| 37 | import com.google.common.base.Throwables; |
| 38 | import com.google.common.io.ByteStreams; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class BasePayloadSlicer implements PayloadSlicer { |
| 46 | /** |
| 47 | * {@inheritDoc} |
| 48 | */ |
| 49 | @Override |
| 50 | public Payload slice(Payload input, long offset, long length) { |
| 51 | checkNotNull(input); |
| 52 | checkArgument(offset >= 0, "offset is negative"); |
| 53 | checkArgument(length >= 0, "length is negative"); |
| 54 | Payload returnVal; |
| 55 | if (input.getRawContent() instanceof File) { |
| 56 | returnVal = doSlice((File) input.getRawContent(), offset, length); |
| 57 | } else if (input.getRawContent() instanceof String) { |
| 58 | returnVal = doSlice((byte[]) input.getRawContent(), offset, length); |
| 59 | } else if (input.getRawContent() instanceof byte[]) { |
| 60 | returnVal = doSlice((byte[]) input.getRawContent(), offset, length); |
| 61 | } else { |
| 62 | returnVal = doSlice(input.getInput(), offset, length); |
| 63 | } |
| 64 | return copyMetadataAndSetLength(input, returnVal, length); |
| 65 | } |
| 66 | |
| 67 | protected Payload doSlice(Payload content, long offset, long length) { |
| 68 | return new InputStreamSupplierPayload(ByteStreams.slice(content, offset, length)); |
| 69 | } |
| 70 | |
| 71 | protected Payload doSlice(String content, long offset, long length) { |
| 72 | return doSlice(content.getBytes(), offset, length); |
| 73 | } |
| 74 | |
| 75 | protected Payload doSlice(File content, long offset, long length) { |
| 76 | try { |
| 77 | return doSlice(new FileInputStream(content), offset, length); |
| 78 | } catch (FileNotFoundException e) { |
| 79 | Throwables.propagate(e); |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected Payload doSlice(InputStream content, long offset, long length) { |
| 85 | return new InputStreamSupplierPayload(ByteStreams.slice(InputSuppliers.of(content), offset, length)); |
| 86 | } |
| 87 | |
| 88 | protected Payload doSlice(byte[] content, long offset, long length) { |
| 89 | Payload returnVal; |
| 90 | checkArgument(offset <= Integer.MAX_VALUE, "offset is too big for an array"); |
| 91 | checkArgument(length <= Integer.MAX_VALUE, "length is too big for an array"); |
| 92 | returnVal = new InputStreamSupplierPayload( |
| 93 | ByteStreams.newInputStreamSupplier(content, (int) offset, (int) length)); |
| 94 | return returnVal; |
| 95 | } |
| 96 | |
| 97 | protected Payload copyMetadataAndSetLength(Payload input, Payload returnVal, long length) { |
| 98 | returnVal.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(input.getContentMetadata() |
| 99 | .toBuilder().contentLength(length).contentMD5(null).build())); |
| 100 | return returnVal; |
| 101 | } |
| 102 | |
| 103 | } |