src/share/jaxws_classes/com/sun/xml/internal/ws/util/ReadAllStream.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
31 import java.io.File; 31 import java.io.File;
32 import java.io.FileInputStream; 32 import java.io.FileInputStream;
33 import java.io.FileOutputStream; 33 import java.io.FileOutputStream;
34 import java.io.IOException; 34 import java.io.IOException;
35 import java.io.InputStream; 35 import java.io.InputStream;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
36 38
37 /** 39 /**
38 * Reads a input stream completely and creates a new stream 40 * Reads a input stream completely and creates a new stream
39 * by keeping some data in memory and the rest on the file system. 41 * by keeping some data in memory and the rest on the file system.
40 * 42 *
45 private final @NotNull MemoryStream memStream; 47 private final @NotNull MemoryStream memStream;
46 private final @NotNull FileStream fileStream; 48 private final @NotNull FileStream fileStream;
47 49
48 private boolean readAll; 50 private boolean readAll;
49 private boolean closed; 51 private boolean closed;
52
53 private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName());
50 54
51 public ReadAllStream() { 55 public ReadAllStream() {
52 memStream = new MemoryStream(); 56 memStream = new MemoryStream();
53 fileStream = new FileStream(); 57 fileStream = new FileStream();
54 } 58 }
73 if (!eof) { 77 if (!eof) {
74 fileStream.readAll(in); 78 fileStream.readAll(in);
75 } 79 }
76 } 80 }
77 81
82 @Override
78 public int read() throws IOException { 83 public int read() throws IOException {
79 int ch = memStream.read(); 84 int ch = memStream.read();
80 if (ch == -1) { 85 if (ch == -1) {
81 ch = fileStream.read(); 86 ch = fileStream.read();
82 } 87 }
90 len = fileStream.read(b, off, sz); 95 len = fileStream.read(b, off, sz);
91 } 96 }
92 return len; 97 return len;
93 } 98 }
94 99
100 @Override
95 public void close() throws IOException { 101 public void close() throws IOException {
96 if (!closed) { 102 if (!closed) {
97 memStream.close(); 103 memStream.close();
98 fileStream.close(); 104 fileStream.close();
99 closed = true; 105 closed = true;
118 fileOut.close(); 124 fileOut.close();
119 } 125 }
120 fin = new FileInputStream(tempFile); 126 fin = new FileInputStream(tempFile);
121 } 127 }
122 128
129 @Override
123 public int read() throws IOException { 130 public int read() throws IOException {
124 return (fin != null) ? fin.read() : -1; 131 return (fin != null) ? fin.read() : -1;
125 } 132 }
126 133
127 @Override 134 @Override
133 public void close() throws IOException { 140 public void close() throws IOException {
134 if (fin != null) { 141 if (fin != null) {
135 fin.close(); 142 fin.close();
136 } 143 }
137 if (tempFile != null) { 144 if (tempFile != null) {
138 tempFile.delete(); 145 boolean success = tempFile.delete();
146 if (!success) {
147 LOGGER.log(Level.INFO, "File {0} could not be deleted", tempFile);
148 }
139 } 149 }
140 } 150 }
141 } 151 }
142 152
143 // Keeps data in memory until certain size 153 // Keeps data in memory until certain size
166 long total = 0; 176 long total = 0;
167 while(true) { 177 while(true) {
168 byte[] buf = new byte[8192]; 178 byte[] buf = new byte[8192];
169 int read = fill(in, buf); 179 int read = fill(in, buf);
170 total += read; 180 total += read;
171 if (read != 0) 181 if (read != 0) {
172 add(buf, read); 182 add(buf, read);
173 if (read != buf.length) 183 }
174 return true; // EOF 184 if (read != buf.length) {
175 if (total > inMemory) 185 return true;
176 return false; // Reached in-memory size 186 } // EOF
187 if (total > inMemory) {
188 return false; // Reached in-memory size
189 }
177 } 190 }
178 } 191 }
179 192
180 private int fill(InputStream in, byte[] buf) throws IOException { 193 private int fill(InputStream in, byte[] buf) throws IOException {
181 int read; 194 int read;
184 total += read; 197 total += read;
185 } 198 }
186 return total; 199 return total;
187 } 200 }
188 201
202 @Override
189 public int read() throws IOException { 203 public int read() throws IOException {
190 if (!fetch()) { 204 if (!fetch()) {
191 return -1; 205 return -1;
192 } 206 }
193 return (head.buf[curOff++] & 0xff); 207 return (head.buf[curOff++] & 0xff);

mercurial