

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# `ContentStreamProvider`에서 구현 AWS SDK for Java 2.x
<a name="content-stream-provider"></a>

`ContentStreamProvider`는에서 입력 데이터의 여러 읽기 AWS SDK for Java 2.x 를 허용하는 데 사용되는 추상화입니다. 이 주제에서는 애플리케이션에서 `ContentStreamProvider`를 올바르게 구현하는 방법을 설명합니다.

SDK for Java 2.x는 전체 스트림을 읽어야 할 때마다 `ContentStreamProvider#newStream()` 메서드를 사용합니다. 전체 스트림에서 작동하려면 반환된 스트림이 항상 콘텐츠의 시작 부분에 있어야 하며 동일한 데이터를 포함해야 합니다.

다음 섹션에서는 이 동작을 올바르게 구현하는 방법에 대한 3가지 접근 방식을 제공합니다.

## `mark()` 및 `reset()` 사용
<a name="csp-impl-mark-reset"></a>

아래 예제에서는 읽기를 시작하기 전에 생성자에서 `mark(int)`를 사용하여 스트림을 다시 시작으로 재설정할 수 있도록 합니다. `newStream()`을 간접 호출할 때마다 스트림이 재설정됩니다.

```
public class MyContentStreamProvider implements ContentStreamProvider {  
    private InputStream contentStream;  
  
    public MyContentStreamProvider(InputStream contentStream) {  
        this.contentStream = contentStream;  
        this.contentStream.mark(MAX_LEN);  
    }  
  
    @Override  
    public InputStream newStream() {  
        contentStream.reset();  
        return contentStream;  
    }  
}
```

## `mark()` 및 `reset()`을 사용할 수 없는 경우 버퍼링 사용
<a name="csp-impl-unsupported-streams"></a>

 스트림이 `mark()` 및 `reset()`을 직접 지원하지 않는 경우 먼저 스트림을 `BufferedInputStream`에 래핑하여 이전에 표시된 솔루션을 계속 사용할 수 있습니다.

```
public class MyContentStreamProvider implements ContentStreamProvider {  
    private BufferedReader contentStream;  
  
    public MyContentStreamProvider(InputStream contentStream) {  
        this.contentStream = new BufferedInputStream(contentStream);  
        this.contentStream.mark(MAX_LEN);
    }  
  
    @Override  
    public InputStream newStream() {  
        contentStream.reset();  
        return contentStream;  
    }  
}
```

## 새 스트림 만들기
<a name="csp-impl-new-stream"></a>

더 간단한 접근 방식은 각 간접 호출에서 데이터에 대한 새 스트림을 얻고 이전 스트림을 종료하는 것입니다.

```
public class MyContentStreamProvider implements ContentStreamProvider {  
    private InputStream contentStream;  
  
    @Override  
    public InputStream newStream() {  
        if (contentStream != null) {  
            contentStream.close();  
        }  
        contentStream = openStream();  
        return contentStream;  
    }  
}
```