All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.client.util.InputStreamContentProvider Maven / Gradle / Ivy

There is a newer version: 11.0.0.beta1
Show newest version
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.client.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.eclipse.jetty.client.api.ContentProvider;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * A {@link ContentProvider} for an {@link InputStream}.
 * 

* The input stream is read once and therefore fully consumed. * Invocations to the {@link #iterator()} method after the first will return an "empty" iterator * because the stream has been consumed on the first invocation. *

* However, it is possible for subclasses to override {@link #onRead(byte[], int, int)} to copy * the content read from the stream to another location (for example a file), and be able to * support multiple invocations of {@link #iterator()}, returning the iterator provided by this * class on the first invocation, and an iterator on the bytes copied to the other location * for subsequent invocations. *

* It is possible to specify, at the constructor, a buffer size used to read content from the * stream, by default 4096 bytes. *

* The {@link InputStream} passed to the constructor is by default closed when is it fully * consumed (or when an exception is thrown while reading it), unless otherwise specified * to the {@link #InputStreamContentProvider(java.io.InputStream, int, boolean) constructor}. */ public class InputStreamContentProvider implements ContentProvider { private static final Logger LOG = Log.getLogger(InputStreamContentProvider.class); private final InputStream stream; private final int bufferSize; private final boolean autoClose; public InputStreamContentProvider(InputStream stream) { this(stream, 4096); } public InputStreamContentProvider(InputStream stream, int bufferSize) { this(stream, bufferSize, true); } public InputStreamContentProvider(InputStream stream, int bufferSize, boolean autoClose) { this.stream = stream; this.bufferSize = bufferSize; this.autoClose = autoClose; } @Override public long getLength() { return -1; } /** * Callback method invoked just after having read from the stream, * but before returning the iteration element (a {@link ByteBuffer} * to the caller. *

* Subclasses may override this method to copy the content read from * the stream to another location (a file, or in memory if the content * is known to fit). * * @param buffer the byte array containing the bytes read * @param offset the offset from where bytes should be read * @param length the length of the bytes read * @return a {@link ByteBuffer} wrapping the byte array */ protected ByteBuffer onRead(byte[] buffer, int offset, int length) { if (length <= 0) return BufferUtil.EMPTY_BUFFER; return ByteBuffer.wrap(buffer, offset, length); } @Override public Iterator iterator() { return new Iterator() { private final byte[] bytes = new byte[bufferSize]; private Exception failure; private ByteBuffer buffer; @Override public boolean hasNext() { try { int read = stream.read(bytes); if (read > 0) { buffer = onRead(bytes, 0, read); return true; } else if (read < 0) { close(); return false; } else { buffer = BufferUtil.EMPTY_BUFFER; return true; } } catch (Exception x) { if (failure == null) { failure = x; // Signal we have more content to cause a call to // next() which will throw NoSuchElementException. close(); return true; } return false; } } @Override public ByteBuffer next() { ByteBuffer result = buffer; buffer = null; if (failure != null) throw (NoSuchElementException)new NoSuchElementException().initCause(failure); if (result == null) throw new NoSuchElementException(); return result; } @Override public void remove() { throw new UnsupportedOperationException(); } private void close() { if (autoClose) { try { stream.close(); } catch (IOException x) { LOG.ignore(x); } } } }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy