Example usage for java.io InputStream InputStream

List of usage examples for java.io InputStream InputStream

Introduction

In this page you can find the example usage for java.io InputStream InputStream.

Prototype

InputStream

Source Link

Usage

From source file:org.lnicholls.galleon.util.Tools.java

public static BufferedImage ImageIORead(File file) {
    System.gc();//from  w  w w.j a v  a  2s  .co m
    try {
        FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
        final ByteBuffer buf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());
        return ImageIO.read(new InputStream() {
            public synchronized int read() throws IOException {
                if (!buf.hasRemaining()) {
                    return -1;
                }
                return buf.get();
            }

            public synchronized int read(byte[] bytes, int off, int len) throws IOException {
                if (!buf.hasRemaining()) {
                    return -1;
                }
                len = Math.min(len, buf.remaining());
                buf.get(bytes, off, len);
                return len;
            }
        });
    } catch (Exception ex) {
        Tools.logException(Tools.class, ex, file.getAbsolutePath());
    }

    try {
        return ImageIO.read(new FileInputStream(file));
    } catch (Exception ex) {
        Tools.logException(Tools.class, ex, file.getAbsolutePath());
    }
    return null;
}

From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java

private void createEmptyObject(final String bucketName, final String objectName)
        throws AmazonClientException, AmazonServiceException {
    final InputStream im = new InputStream() {
        @Override/*from   w  w w .j av a2s  . co m*/
        public int read() throws IOException {
            return -1;
        }
    };

    final ObjectMetadata om = new ObjectMetadata();
    om.setContentLength(0L);
    if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) {
        om.setServerSideEncryption(serverSideEncryptionAlgorithm);
    }
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om);
    putObjectRequest.setCannedAcl(cannedACL);
    s3.putObject(putObjectRequest);
    statistics.incrementWriteOps(1);
}

From source file:org.commoncrawl.hadoop.mergeutils.MergeSortSpillWriter.java

public static InputStream newInputStream(final ByteBuffer buf) {
    return new InputStream() {
        public synchronized int read() throws IOException {
            if (!buf.hasRemaining()) {
                return -1;
            }//w ww .j  a v a  2 s.  c  om
            return buf.get() & 0xff;
        }

        public synchronized int read(byte[] bytes, int off, int len) throws IOException {
            // Read only what's left
            len = Math.min(len, buf.remaining());
            buf.get(bytes, off, len);
            return len;
        }
    };
}

From source file:org.apache.hadoop.fs.s3a.S3AFileSystem.java

private void createEmptyObject(final String objectName)
        throws AmazonClientException, AmazonServiceException, InterruptedIOException {
    final InputStream im = new InputStream() {
        @Override/*from w ww .  j av  a  2  s .  c o  m*/
        public int read() throws IOException {
            return -1;
        }
    };

    PutObjectRequest putObjectRequest = newPutObjectRequest(objectName, newObjectMetadata(0L), im);
    Upload upload = putObject(putObjectRequest);
    try {
        upload.waitForUploadResult();
    } catch (InterruptedException e) {
        throw new InterruptedIOException("Interrupted creating " + objectName);
    }
    incrementPutProgressStatistics(objectName, 0);
    instrumentation.directoryCreated();
}

From source file:org.apache.nifi.controller.repository.StandardProcessSession.java

@Override
public InputStream read(final FlowFile source) {
    validateRecordState(source);//from  www . j a  va 2s.c  om
    final StandardRepositoryRecord record = records.get(source);

    try {
        ensureNotAppending(record.getCurrentClaim());
    } catch (final IOException e) {
        throw new FlowFileAccessException("Failed to access ContentClaim for " + source.toString(), e);
    }

    final InputStream rawIn = getInputStream(source, record.getCurrentClaim(), record.getCurrentClaimOffset(),
            false);
    final InputStream limitedIn = new LimitedInputStream(rawIn, source.getSize());
    final ByteCountingInputStream countingStream = new ByteCountingInputStream(limitedIn, this.bytesRead);
    final FlowFileAccessInputStream ffais = new FlowFileAccessInputStream(countingStream, source,
            record.getCurrentClaim());

    final InputStream errorHandlingStream = new InputStream() {

        @Override
        public int read() throws IOException {
            try {
                return ffais.read();
            } catch (final ContentNotFoundException cnfe) {
                handleContentNotFound(cnfe, record);
                close();
                throw cnfe;
            } catch (final FlowFileAccessException ffae) {
                LOG.error("Failed to read content from " + source + "; rolling back session", ffae);
                rollback(true);
                close();
                throw ffae;
            }
        }

        @Override
        public int read(final byte[] b) throws IOException {
            return read(b, 0, b.length);
        }

        @Override
        public int read(final byte[] b, final int off, final int len) throws IOException {
            try {
                return ffais.read(b, off, len);
            } catch (final ContentNotFoundException cnfe) {
                handleContentNotFound(cnfe, record);
                close();
                throw cnfe;
            } catch (final FlowFileAccessException ffae) {
                LOG.error("Failed to read content from " + source + "; rolling back session", ffae);
                rollback(true);
                close();
                throw ffae;
            }
        }

        @Override
        public void close() throws IOException {
            StandardProcessSession.this.bytesRead += countingStream.getBytesRead();

            ffais.close();
            openInputStreams.remove(source);
        }

        @Override
        public int available() throws IOException {
            return ffais.available();
        }

        @Override
        public long skip(long n) throws IOException {
            return ffais.skip(n);
        }

        @Override
        public boolean markSupported() {
            return ffais.markSupported();
        }

        @Override
        public synchronized void mark(int readlimit) {
            ffais.mark(readlimit);
        }

        @Override
        public synchronized void reset() throws IOException {
            ffais.reset();
        }

        @Override
        public String toString() {
            return "ErrorHandlingInputStream[FlowFile=" + source + "]";
        }
    };

    openInputStreams.put(source, errorHandlingStream);
    return errorHandlingStream;
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static Object basicReadObject(final DataInput in) throws IOException, ClassNotFoundException {
    checkIn(in);// w  w  w. j a  v  a  2  s  .c om

    // Read the header byte
    byte header = in.readByte();
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "basicReadObject: header={}", header);
    }
    switch (header) {
    case DS_FIXED_ID_BYTE:
        return DSFIDFactory.create(in.readByte(), in);
    case DS_FIXED_ID_SHORT:
        return DSFIDFactory.create(in.readShort(), in);
    case DS_FIXED_ID_INT:
        return DSFIDFactory.create(in.readInt(), in);
    case DS_NO_FIXED_ID:
        return readDataSerializableFixedID(in);
    case NULL:
        return null;
    case NULL_STRING:
    case STRING:
    case HUGE_STRING:
    case STRING_BYTES:
    case HUGE_STRING_BYTES:
        return readString(in, header);
    case CLASS:
        return readClass(in);
    case DATE:
        return readDate(in);
    case FILE:
        return readFile(in);
    case INET_ADDRESS:
        return readInetAddress(in);
    case BOOLEAN:
        return readBoolean(in);
    case CHARACTER:
        return readCharacter(in);
    case BYTE:
        return readByte(in);
    case SHORT:
        return readShort(in);
    case INTEGER:
        return readInteger(in);
    case LONG:
        return readLong(in);
    case FLOAT:
        return readFloat(in);
    case DOUBLE:
        return readDouble(in);
    case BYTE_ARRAY:
        return readByteArray(in);
    case ARRAY_OF_BYTE_ARRAYS:
        return readArrayOfByteArrays(in);
    case SHORT_ARRAY:
        return readShortArray(in);
    case STRING_ARRAY:
        return readStringArray(in);
    case INT_ARRAY:
        return readIntArray(in);
    case LONG_ARRAY:
        return readLongArray(in);
    case FLOAT_ARRAY:
        return readFloatArray(in);
    case DOUBLE_ARRAY:
        return readDoubleArray(in);
    case BOOLEAN_ARRAY:
        return readBooleanArray(in);
    case CHAR_ARRAY:
        return readCharArray(in);
    case OBJECT_ARRAY:
        return readObjectArray(in);
    case ARRAY_LIST:
        return readArrayList(in);
    case LINKED_LIST:
        return readLinkedList(in);
    case HASH_SET:
        return readHashSet(in);
    case LINKED_HASH_SET:
        return readLinkedHashSet(in);
    case HASH_MAP:
        return readHashMap(in);
    case IDENTITY_HASH_MAP:
        return readIdentityHashMap(in);
    case HASH_TABLE:
        return readHashtable(in);
    case CONCURRENT_HASH_MAP:
        return readConcurrentHashMap(in);
    case PROPERTIES:
        return readProperties(in);
    case TIME_UNIT:
        return readTimeUnit(in);
    case USER_CLASS:
        return readUserObject(in, in.readByte());
    case USER_CLASS_2:
        return readUserObject(in, in.readShort());
    case USER_CLASS_4:
        return readUserObject(in, in.readInt());
    case VECTOR:
        return readVector(in);
    case STACK:
        return readStack(in);
    case TREE_MAP:
        return readTreeMap(in);
    case TREE_SET:
        return readTreeSet(in);
    case BOOLEAN_TYPE:
        return Boolean.TYPE;
    case CHARACTER_TYPE:
        return Character.TYPE;
    case BYTE_TYPE:
        return Byte.TYPE;
    case SHORT_TYPE:
        return Short.TYPE;
    case INTEGER_TYPE:
        return Integer.TYPE;
    case LONG_TYPE:
        return Long.TYPE;
    case FLOAT_TYPE:
        return Float.TYPE;
    case DOUBLE_TYPE:
        return Double.TYPE;
    case VOID_TYPE:
        return Void.TYPE;

    case USER_DATA_SERIALIZABLE:
        return readUserDataSerializable(in, in.readByte());
    case USER_DATA_SERIALIZABLE_2:
        return readUserDataSerializable(in, in.readShort());
    case USER_DATA_SERIALIZABLE_4:
        return readUserDataSerializable(in, in.readInt());

    case DATA_SERIALIZABLE:
        return readDataSerializable(in);

    case SERIALIZABLE: {
        final boolean isDebugEnabled_SERIALIZER = logger.isTraceEnabled(LogMarker.SERIALIZER);
        Object serializableResult;
        if (in instanceof DSObjectInputStream) {
            serializableResult = ((DSObjectInputStream) in).readObject();
        } else {
            InputStream stream;
            if (in instanceof InputStream) {
                stream = (InputStream) in;
            } else {
                stream = new InputStream() {
                    @Override
                    public int read() throws IOException {
                        try {
                            return in.readUnsignedByte(); // fix for bug 47249
                        } catch (EOFException ignored) {
                            return -1;
                        }
                    }

                };
            }

            ObjectInput ois = new DSObjectInputStream(stream);
            if (stream instanceof VersionedDataStream) {
                Version v = ((VersionedDataStream) stream).getVersion();
                if (v != null && v != Version.CURRENT) {
                    ois = new VersionedObjectInput(ois, v);
                }
            }

            serializableResult = ois.readObject();

            if (isDebugEnabled_SERIALIZER) {
                logger.trace(LogMarker.SERIALIZER, "Read Serializable object: {}", serializableResult);
            }
        }
        if (isDebugEnabled_SERIALIZER) {
            logger.trace(LogMarker.SERIALIZER, "deserialized instanceof {}", serializableResult.getClass());
        }
        return serializableResult;
    }
    case PDX:
        return readPdxSerializable(in);
    case PDX_ENUM:
        return readPdxEnum(in);
    case GEMFIRE_ENUM:
        return readGemFireEnum(in);
    case PDX_INLINE_ENUM:
        return readPdxInlineEnum(in);
    case BIG_INTEGER:
        return readBigInteger(in);
    case BIG_DECIMAL:
        return readBigDecimal(in);
    case UUID:
        return readUUID(in);
    case TIMESTAMP:
        return readTimestamp(in);
    default:
        String s = "Unknown header byte: " + header;
        throw new IOException(s);
    }
}