org.jvnet.mimepull.FileData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservices-rt Show documentation
Show all versions of webservices-rt Show documentation
This module contains the Metro runtime code.
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.jvnet.mimepull;
import java.nio.ByteBuffer;
/**
* Keeps the Part's partial content data in a file.
*
* @author Kohsuke Kawaguchi
* @author Jitendra Kotamraju
*/
final class FileData implements Data {
private final DataFile file;
private final long pointer; // read position
private final int length;
FileData(DataFile file, ByteBuffer buf) {
this(file, file.writeTo(buf.array(), 0, buf.limit()), buf.limit());
}
FileData(DataFile file, long pointer, int length) {
this.file = file;
this.pointer = pointer;
this.length = length;
}
@Override
public byte[] read() {
byte[] buf = new byte[length];
file.read(pointer, buf, 0, length);
return buf;
}
/*
* This shouldn't be called
*/
@Override
public long writeTo(DataFile file) {
throw new IllegalStateException();
}
@Override
public int size() {
return length;
}
/*
* Always create FileData
*/
@Override
public Data createNext(DataHead dataHead, ByteBuffer buf) {
return new FileData(file, buf);
}
}