EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.io.internal]

COVERAGE SUMMARY FOR SOURCE FILE [BasePayloadSlicer.java]

nameclass, %method, %block, %line, %
BasePayloadSlicer.java0%   (0/1)0%   (0/8)0%   (0/161)0%   (0/25)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BasePayloadSlicer0%   (0/1)0%   (0/8)0%   (0/161)0%   (0/25)
BasePayloadSlicer (): void 0%   (0/1)0%   (0/3)0%   (0/1)
copyMetadataAndSetLength (Payload, Payload, long): Payload 0%   (0/1)0%   (0/14)0%   (0/2)
doSlice (File, long, long): Payload 0%   (0/1)0%   (0/15)0%   (0/4)
doSlice (InputStream, long, long): Payload 0%   (0/1)0%   (0/9)0%   (0/1)
doSlice (Payload, long, long): Payload 0%   (0/1)0%   (0/8)0%   (0/1)
doSlice (String, long, long): Payload 0%   (0/1)0%   (0/7)0%   (0/1)
doSlice (byte [], long, long): Payload 0%   (0/1)0%   (0/30)0%   (0/4)
slice (Payload, long, long): Payload 0%   (0/1)0%   (0/75)0%   (0/11)

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 */
19package org.jclouds.io.internal;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23 
24import java.io.File;
25import java.io.FileInputStream;
26import java.io.FileNotFoundException;
27import java.io.InputStream;
28 
29import javax.inject.Singleton;
30 
31import org.jclouds.io.InputSuppliers;
32import org.jclouds.io.Payload;
33import org.jclouds.io.PayloadSlicer;
34import org.jclouds.io.payloads.BaseMutableContentMetadata;
35import org.jclouds.io.payloads.InputStreamSupplierPayload;
36 
37import com.google.common.base.Throwables;
38import com.google.common.io.ByteStreams;
39 
40/**
41 * 
42 * @author Adrian Cole
43 */
44@Singleton
45public 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}

[all classes][org.jclouds.io.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov