private void sendHtmlResponse(@NotNull HttpRequest request, @NotNull ChannelHandlerContext context, String pagePath) throws IOException { BufferExposingByteArrayOutputStream byteOut = new BufferExposingByteArrayOutputStream(); InputStream pageTemplateStream = getClass().getResourceAsStream(pagePath); String pageTemplate = StreamUtil.readText(pageTemplateStream, Charset.forName("UTF-8")); try { String pageWithProductName = pageTemplate.replaceAll("%IDE_NAME", ApplicationNamesInfo.getInstance().getFullProductName()); byteOut.write(StreamUtil.loadFromStream(new ByteArrayInputStream(pageWithProductName.getBytes(Charset.forName("UTF-8"))))); HttpResponse response = Responses.response("text/html", Unpooled.wrappedBuffer(byteOut.getInternalBuffer(), 0, byteOut.size())); Responses.addNoCache(response); response.headers().set("X-Frame-Options", "Deny"); Responses.send(response, context.channel(), request); } finally { byteOut.close(); pageTemplateStream.close(); } }
private void saveFsState(File dataStorageRoot, BuildFSState state) { final ProjectDescriptor pd = myProjectDescriptor; final File file = new File(dataStorageRoot, FS_STATE_FILE); try { final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(bytes); try { out.writeInt(BuildFSState.VERSION); out.writeLong(myLastEventOrdinal); out.writeBoolean(hasWorkToDo(state, pd)); state.save(out); } finally { out.close(); } saveOnDisk(bytes, file); } catch (Throwable e) { LOG.error(e); FileUtil.delete(file); } }
private static void saveOnDisk(BufferExposingByteArrayOutputStream bytes, final File file) throws IOException { FileOutputStream fos = null; try { //noinspection IOResourceOpenedButNotSafelyClosed fos = new FileOutputStream(file); } catch (FileNotFoundException ignored) { FileUtil.createIfDoesntExist(file); } if (fos == null) { fos = new FileOutputStream(file); } try { fos.write(bytes.getInternalBuffer(), 0, bytes.size()); } finally { fos.close(); } }
private BufferExposingByteArrayOutputStream internalReadStream(int record) throws IOException { waitForPendingWriteForRecord(record); byte[] result; synchronized (myLock) { result = super.readBytes(record); } InflaterInputStream in = new CustomInflaterInputStream(result); try { final BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); StreamUtil.copyStreamContent(in, outputStream); return outputStream; } finally { in.close(); } }
private void zipAndWrite(ByteSequence bytes, int record, boolean fixedSize) throws IOException { BufferExposingByteArrayOutputStream s = new BufferExposingByteArrayOutputStream(); DeflaterOutputStream out = new DeflaterOutputStream(s); try { out.write(bytes.getBytes(), bytes.getOffset(), bytes.getLength()); } finally { out.close(); } synchronized (myLock) { doWrite(record, fixedSize, s); myPendingWriteRequestsSize -= bytes.getLength(); myPendingWriteRequests.remove(record); } }
@Override protected void saveChunk(BufferExposingByteArrayOutputStream compressedChunk, long endOfFileOffset) throws IOException { FileAccessorCache.Handle<DataOutputStream> streamCacheValue = ourAppendersCache.get(myPath); try { streamCacheValue.get().write(compressedChunk.getInternalBuffer(), 0, compressedChunk.size()); } finally { streamCacheValue.release(); } streamCacheValue = ourAppendersCache.get(myPath + INCOMPLETE_CHUNK_LENGTH_FILE_EXTENSION); try { DataInputOutputUtil.writeINT(streamCacheValue.get(), compressedChunk.size()); } finally { streamCacheValue.release(); } }
@Override public void force() { if (! myDirty) return; try{ final BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(); final DataOutput out = new DataOutputStream(bos); out.writeInt(myMap.size()); for (Map.Entry<KeyWrapper<K>, V> entry : myMap.entrySet()) { myKeyDescriptor.save(out, entry.getKey().myKey); myValueExternalizer.save(out, entry.getValue()); } FileUtil.writeToFile(myFile, bos.getInternalBuffer(), 0, bos.size()); } catch (IOException e) { LOG.error(e); } finally { myDirty = false; } }
public void write(@NotNull DataOutputStream out) throws IOException { out.writeUTF(getPath()); BufferExposingByteArrayOutputStream bs = new BufferExposingByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bs); try { os.writeObject(myState); out.writeInt(bs.size()); out.write(bs.getInternalBuffer(), 0, bs.size()); } finally { os.close(); bs.close(); } }
@Nullable public String getContent() throws VcsException { if (myContents == null) { BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(2048); try { myRepository.getFile(myPath, -1, null, bos); myRepository.closeSession(); } catch (SVNException e) { throw new VcsException(e); } final byte[] bytes = bos.toByteArray(); final Charset charset = myFilePath.getCharset(); myContents = CharsetToolkit.bytesToString(bytes, charset); } return myContents; }
private void saveFsState(File dataStorageRoot, BuildFSState state) { final ProjectDescriptor pd = myProjectDescriptor; final File file = new File(dataStorageRoot, FS_STATE_FILE); try { final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(bytes); try { out.writeInt(FSState.VERSION); out.writeLong(myLastEventOrdinal); out.writeBoolean(hasWorkToDo(state, pd)); state.save(out); } finally { out.close(); } saveOnDisk(bytes, file); } catch (Throwable e) { LOG.error(e); FileUtil.delete(file); } }
private static void saveOnDisk(BufferExposingByteArrayOutputStream bytes, final File file) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { FileUtil.createIfDoesntExist(file); } if (fos == null) { fos = new FileOutputStream(file); } try { fos.write(bytes.getInternalBuffer(), 0, bytes.size()); } finally { fos.close(); } }
@Override public void close() throws IOException { super.close(); try { synchronized (myAttributeId) { final BufferExposingByteArrayOutputStream _out = (BufferExposingByteArrayOutputStream)out; final int page; try { w.lock(); incModCount(myFileId); page = findAttributePage(myFileId, myAttributeId, true); } finally { w.unlock(); } getAttributesStorage().writeBytes(page, new ByteSequence(_out.getInternalBuffer(), 0, _out.size()), myFixedSize); } } catch (Throwable e) { throw DbConnection.handleError(e); } }
private BufferExposingByteArrayOutputStream internalReadStream(int record) throws IOException { waitForPendingWriteForRecord(record); synchronized (myLock) { byte[] result = super.readBytes(record); InflaterInputStream in = new CustomInflaterInputStream(result); try { final BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); StreamUtil.copyStreamContent(in, outputStream); return outputStream; } finally { in.close(); } } }
@Nullable public String getContent() throws VcsException { if (myContents == null) { BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(2048); try { myRepository.getFile(myPath, -1, null, bos); myRepository.closeSession(); } catch (SVNException e) { throw new VcsException(e); } final byte[] bytes = bos.toByteArray(); final Charset charset = myFilePath.getCharset(); myContents = charset == null ? CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset()) : CharsetToolkit.bytesToString(bytes, charset); } return myContents; }
@Nonnull private static byte[] archiveState(@Nonnull Element state) { BufferExposingByteArrayOutputStream byteOut = new BufferExposingByteArrayOutputStream(); try { OutputStreamWriter writer = new OutputStreamWriter(new SnappyOutputStream(byteOut), CharsetToolkit.UTF8_CHARSET); try { XMLOutputter xmlOutputter = new JDOMUtil.MyXMLOutputter(); xmlOutputter.setFormat(XML_FORMAT); xmlOutputter.output(state, writer); } finally { writer.close(); } } catch (IOException e) { throw new StateStorageException(e); } return ArrayUtil.realloc(byteOut.getInternalBuffer(), byteOut.size()); }
private static void updateFsStateOnDisk(File dataStorageRoot, DataInputStream original, final long ordinal) { final File file = new File(dataStorageRoot, FS_STATE_FILE); try { final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(bytes); try { out.writeInt(BuildFSState.VERSION); out.writeLong(ordinal); out.writeBoolean(false); while (true) { final int b = original.read(); if (b == -1) { break; } out.write(b); } } finally { out.close(); } saveOnDisk(bytes, file); } catch (Throwable e) { LOG.error(e); FileUtil.delete(file); } }
@NotNull public String readString(@Nullable final ProgressIndicator indicator) throws IOException { return connect(new HttpRequests.RequestProcessor<String>() { @Override public String process(@NotNull HttpRequests.Request request) throws IOException { int contentLength = request.getConnection().getContentLength(); BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(contentLength > 0 ? contentLength : 16 * 1024); NetUtils.copyStreamContent(indicator, request.getInputStream(), out, contentLength); return new String(out.getInternalBuffer(), 0, out.size(), HttpRequests.getCharset(request)); } }); }
@Override public void close() throws IOException { super.close(); try { final BufferExposingByteArrayOutputStream _out = (BufferExposingByteArrayOutputStream)out; writeBytes(new ByteSequence(_out.getInternalBuffer(), 0, _out.size())); } catch (Throwable e) { throw DbConnection.handleError(e); } }
public static Image loadFromStream(@NotNull final InputStream inputStream, final int scale) { if (scale <= 0) throw new IllegalArgumentException("Scale must 1 or more"); try { BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; while (true) { final int n = inputStream.read(buffer); if (n < 0) break; outputStream.write(buffer, 0, n); } } finally { inputStream.close(); } Image image = Toolkit.getDefaultToolkit().createImage(outputStream.getInternalBuffer(), 0, outputStream.size()); waitForImage(image); if (UIUtil.isRetina() && scale > 1) { image = RetinaImage.createFrom(image, scale, ourComponent); } return image; } catch (Exception ex) { LOG.error(ex); } return null; }
public void putNextEntryBytes(JBZipEntry entry, byte[] bytes) throws IOException { entry.setSize(bytes.length); crc.reset(); crc.update(bytes); entry.setCrc(crc.getValue()); if (entry.getMethod() == -1) { entry.setMethod(method); } if (entry.getTime() == -1) { entry.setTime(System.currentTimeMillis()); } final byte[] outputBytes; final int outputBytesLength; if (entry.getMethod() == ZipEntry.DEFLATED) { def.setLevel(level); final BufferExposingByteArrayOutputStream compressedBytesStream = new BufferExposingByteArrayOutputStream(); final DeflaterOutputStream stream = new DeflaterOutputStream(compressedBytesStream, def); try { stream.write(bytes); } finally { stream.close(); } outputBytesLength = compressedBytesStream.size(); outputBytes = compressedBytesStream.getInternalBuffer(); } else { outputBytesLength = bytes.length; outputBytes = bytes; } entry.setCompressedSize(outputBytesLength); writeLocalFileHeader(entry); writeOut(outputBytes, 0, outputBytesLength); }
public <Data> int append(Data value, KeyDescriptor<Data> descriptor) throws IOException { final BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(); DataOutput out = new DataOutputStream(bos); descriptor.save(out, value); final int size = bos.size(); final byte[] buffer = bos.getInternalBuffer(); int currentLength = getCurrentLength(); if (myCompressedAppendableFile != null) { //myCompressedAppendableFile.append(value, descriptor); myCompressedAppendableFile.append(buffer, size); if (!testMode) return currentLength; } if (size > ourAppendBufferLength) { flushKeyStoreBuffer(); put(currentLength, buffer, 0, size); myFileLength += size; } else { if (size > ourAppendBufferLength - myBufferPosition) { flushKeyStoreBuffer(); } // myAppendBuffer will contain complete records if (myAppendBuffer == null) { myAppendBuffer = new byte[ourAppendBufferLength]; } System.arraycopy(buffer, 0, myAppendBuffer, myBufferPosition, size); myBufferPosition += size; } return currentLength; }
@Override protected void saveChunk(BufferExposingByteArrayOutputStream compressedChunk, long endOfFileOffset) throws IOException { AppendableStorageBackedByResizableMappedFile.super .put(endOfFileOffset, compressedChunk.getInternalBuffer(), 0, compressedChunk.size()); if (myChunkLengthTableStream == null) { myChunkLengthTableStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunkLengthFile(), true))); } DataInputOutputUtil.writeINT(myChunkLengthTableStream, compressedChunk.size()); }
public synchronized <Data> void append(Data value, KeyDescriptor<Data> descriptor) throws IOException { final BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(); DataOutput out = new DataOutputStream(bos); descriptor.save(out, value); final int size = bos.size(); final byte[] buffer = bos.getInternalBuffer(); append(buffer, size); }
public void serialize(@NotNull Stub rootStub, @NotNull OutputStream stream) throws IOException { BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(); FileLocalStringEnumerator storage = new FileLocalStringEnumerator(true); StubOutputStream stubOutputStream = new StubOutputStream(out, storage); boolean doDefaultSerialization = true; if (rootStub instanceof PsiFileStub) { final PsiFileStub[] roots = ((PsiFileStub)rootStub).getStubRoots(); if (roots.length == 0) { Logger.getInstance(getClass()).error("Incorrect stub files count during serialization:" + rootStub + "," + rootStub.getStubType()); } else { doDefaultSerialization = false; DataInputOutputUtil.writeINT(stubOutputStream, roots.length); for (PsiFileStub root : roots) { doSerialize(root, stubOutputStream); } } } if (doDefaultSerialization) { DataInputOutputUtil.writeINT(stubOutputStream, 1); doSerialize(rootStub, stubOutputStream); } DataOutputStream resultStream = new DataOutputStream(stream); DataInputOutputUtil.writeINT(resultStream, storage.myStrings.size()); byte[] buffer = IOUtil.allocReadWriteUTFBuffer(); for(String s:storage.myStrings) { IOUtil.writeUTFFast(buffer, resultStream, s); } resultStream.write(out.getInternalBuffer(), 0, out.size()); }
@Override protected void doPut(Key key, ValueContainer<Value> container) throws IOException { synchronized (myEnumerator) { ChangeTrackingValueContainer<Value> valueContainer = (ChangeTrackingValueContainer<Value>)container; // try to accumulate index value calculated for particular key to avoid fragmentation: usually keys are scattered across many files // note that keys unique for indexed file have their value calculated at once (e.g. key is file id, index calculates something for particular // file) and there is no benefit to accumulate values for particular key because only one value exists if (!valueContainer.needsCompacting() && !myKeyIsUniqueForIndexedFile) { final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); //noinspection IOResourceOpenedButNotSafelyClosed final DataOutputStream _out = new DataOutputStream(bytes); valueContainer.saveTo(_out, myValueExternalizer); appendData(key, new PersistentHashMap.ValueDataAppender() { @Override public void append(@NotNull final DataOutput out) throws IOException { out.write(bytes.getInternalBuffer(), 0, bytes.size()); } }); } else { // rewrite the value container for defragmentation super.doPut(key, valueContainer); } } }
private static Object createSerializedCopy(final Object original) { // FormLayout may have been loaded with a different classloader, so we need to create a copy through serialization Object copy; try { BufferExposingByteArrayOutputStream baos = new BufferExposingByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(baos); try { os.writeObject(original); } finally { os.close(); } InputStream bais = new ByteArrayInputStream(baos.getInternalBuffer(), 0, baos.size()); ObjectInputStream is = new ObjectInputStream(bais); try { copy = is.readObject(); } finally { is.close(); } } catch (Exception e) { throw new RuntimeException(e); } return copy; }
private static void updateFsStateOnDisk(File dataStorageRoot, DataInputStream original, final long ordinal) { final File file = new File(dataStorageRoot, FS_STATE_FILE); try { final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); final DataOutputStream out = new DataOutputStream(bytes); try { out.writeInt(FSState.VERSION); out.writeLong(ordinal); out.writeBoolean(false); while (true) { final int b = original.read(); if (b == -1) { break; } out.write(b); } } finally { out.close(); } saveOnDisk(bytes, file); } catch (Throwable e) { LOG.error(e); FileUtil.delete(file); } }
public void serialize(@NotNull Stub rootStub, @NotNull OutputStream stream) throws IOException { BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(); FileLocalStringEnumerator storage = new FileLocalStringEnumerator(); StubOutputStream stubOutputStream = new StubOutputStream(out, storage); doSerialize(rootStub, stubOutputStream); DataOutputStream resultStream = new DataOutputStream(stream); DataInputOutputUtil.writeINT(resultStream, storage.myStrings.size()); byte[] buffer = IOUtil.allocReadWriteUTFBuffer(); for(String s:storage.myStrings) { IOUtil.writeUTFFast(buffer, resultStream, s); } resultStream.write(out.getInternalBuffer(), 0, out.size()); }
protected int doWriteData(Data value) throws IOException { final int dataOff = myKeyStorage != null ? myKeyStoreBufferPosition + myKeyStoreFileLength : ((InlineKeyDescriptor<Data>)myDataDescriptor).toInt(value); if (myKeyStorage != null) { final BufferExposingByteArrayOutputStream bos = new BufferExposingByteArrayOutputStream(); DataOutput out = new DataOutputStream(bos); myDataDescriptor.save(out, value); final int size = bos.size(); final byte[] buffer = bos.getInternalBuffer(); if (size > myInitialSize) { flushKeyStoreBuffer(); myKeyStorage.put(dataOff, buffer, 0, size); myKeyStoreFileLength += size; } else { if (size > myInitialSize - myKeyStoreBufferPosition) { flushKeyStoreBuffer(); } // myKeyStoreFileBuffer will contain complete records if (myKeyStoreFileBuffer == null) { myKeyStoreFileBuffer = new byte[myInitialSize]; } System.arraycopy(buffer, 0, myKeyStoreFileBuffer, myKeyStoreBufferPosition, size); myKeyStoreBufferPosition += size; } } return dataOff; }
protected void doPut(Key key, Value value) throws IOException { myEnumerator.lockStorage(); try { myEnumerator.markDirty(true); myAppendCache.remove(key); final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream(); if (myFlyweightAppenderStream == null) myFlyweightAppenderStream = new AppendStream(); myFlyweightAppenderStream.setOut(bytes); myValueExternalizer.save(myFlyweightAppenderStream, value); myFlyweightAppenderStream.setOut(null); final int id = enumerate(key); long oldheader = readValueId(id); if (oldheader != NULL_ADDR) { myLiveAndGarbageKeysCounter++; } else { myLiveAndGarbageKeysCounter += LIVE_KEY_MASK; } long header = myValueStorage.appendBytes(bytes.getInternalBuffer(), 0, bytes.size(), 0); updateValueId(id, header, oldheader, key, 0); } finally { myEnumerator.unlockStorage(); } }