Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66264097c9837e7096842e9316b424c8527e6f14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.emf.cdo.common.lob;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * An {@link OutputStream} that produces a {@link #getBlob() CDOBlob}.
 *
 * @author Christian W. Damus (CEA LIST)
 * @since 4.3
 */
public class CDOBlobOutputStream extends OutputStream
{
  private ByteArrayOutputStream buffer = new ByteArrayOutputStream();

  private CDOBlob blob;

  @Override
  public void write(int b) throws IOException
  {
    checkBuffer();
    buffer.write(b);
  }

  @Override
  public void write(byte[] b, int off, int len) throws IOException
  {
    checkBuffer();
    buffer.write(b, off, len);
  }

  @Override
  public void close() throws IOException
  {
    if (buffer != null)
    {
      blob = new CDOBlob(new ByteArrayInputStream(buffer.toByteArray()));
      buffer = null;
    }
  }

  @Override
  public void flush() throws IOException
  {
    checkBuffer();
    buffer.flush();
  }

  public CDOBlob getBlob()
  {
    return blob;
  }

  void checkBuffer() throws IOException
  {
    if (buffer == null)
    {
      throw new IOException("CDOBlobOutputStream closed"); //$NON-NLS-1$
    }
  }
}

Back to the top