/* * Copyright 2014-2016 CyberVision, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.kaaproject.kaa.client.transport; import org.apache.http.entity.mime.MIME; import org.apache.http.entity.mime.content.AbstractContentBody; import java.io.IOException; import java.io.OutputStream; public class ByteArrayBody extends AbstractContentBody { private final byte[] data; private final String filename; /** * All-args constructor. */ public ByteArrayBody(final byte[] data, final String mimeType, final String filename) { super(mimeType); if (data == null) { throw new IllegalArgumentException("byte[] data may not be null"); } this.data = data; this.filename = filename; } public ByteArrayBody(final byte[] data, final String filename) { this(data, "application/octet-stream", filename); } @Override public String getFilename() { return filename; } @Override public String getCharset() { return null; } @Override public String getTransferEncoding() { return MIME.ENC_BINARY; } @Override public long getContentLength() { return data.length; } @Override public void writeTo(OutputStream out) throws IOException { out.write(data); } }