Skip to content

Commit 180a4e7

Browse files
committed
Merge branch 'master' into pull/726-merged-master
2 parents 292d2f7 + 056cb52 commit 180a4e7

File tree

40 files changed

+1428
-116
lines changed

40 files changed

+1428
-116
lines changed

jdk/src/macosx/classes/sun/lwawt/macosx/CDesktopPeer.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,10 @@
2929
import java.awt.peer.DesktopPeer;
3030
import java.io.File;
3131
import java.io.IOException;
32+
import java.lang.annotation.Native;
3233
import java.net.URI;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
3336

3437

3538
/**
@@ -39,50 +42,76 @@
3942
*/
4043
public class CDesktopPeer implements DesktopPeer {
4144

45+
@Native private static final int OPEN = 0;
46+
@Native private static final int BROWSE = 1;
47+
@Native private static final int EDIT = 2;
48+
@Native private static final int PRINT = 3;
49+
@Native private static final int MAIL = 4;
50+
4251
public boolean isSupported(Action action) {
4352
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
4453
// Though we don't really differentiate between OPEN / EDIT
4554
return true;
4655
}
4756

4857
public void open(File file) throws IOException {
49-
this.lsOpenFile(file, false);
58+
this.lsOpenFile(file, OPEN);
5059
}
5160

5261
public void edit(File file) throws IOException {
53-
this.lsOpenFile(file, false);
62+
this.lsOpenFile(file, EDIT);
5463
}
5564

5665
public void print(File file) throws IOException {
57-
this.lsOpenFile(file, true);
66+
this.lsOpenFile(file, PRINT);
5867
}
5968

6069
public void mail(URI uri) throws IOException {
61-
this.lsOpen(uri);
70+
this.lsOpen(uri, MAIL);
6271
}
6372

6473
public void browse(URI uri) throws IOException {
65-
this.lsOpen(uri);
74+
this.lsOpen(uri, BROWSE);
6675
}
6776

68-
private void lsOpen(URI uri) throws IOException {
69-
int status = _lsOpenURI(uri.toString());
77+
private void lsOpen(URI uri, int action) throws IOException {
78+
int status = _lsOpenURI(uri.toString(), action);
7079

7180
if (status != 0 /* noErr */) {
72-
throw new IOException("Failed to mail or browse " + uri + ". Error code: " + status);
81+
String actionString = (action == MAIL) ? "mail" : "browse";
82+
throw new IOException("Failed to " + actionString + " " + uri
83+
+ ". Error code: " + status);
7384
}
7485
}
7586

76-
private void lsOpenFile(File file, boolean print) throws IOException {
77-
int status = _lsOpenFile(file.getCanonicalPath(), print);
78-
87+
private void lsOpenFile(File file, int action) throws IOException {
88+
int status = -1;
89+
Path tmpFile = null;
90+
String tmpTxtPath = null;
91+
92+
try {
93+
if (action == EDIT) {
94+
tmpFile = Files.createTempFile("TmpFile", ".txt");
95+
tmpTxtPath = tmpFile.toAbsolutePath().toString();
96+
}
97+
status = _lsOpenFile(file.getCanonicalPath(), action, tmpTxtPath);
98+
} catch (Exception e) {
99+
throw new IOException("Failed to create tmp file: ", e);
100+
} finally {
101+
if (tmpFile != null) {
102+
Files.deleteIfExists(tmpFile);
103+
}
104+
}
79105
if (status != 0 /* noErr */) {
80-
throw new IOException("Failed to open, edit or print " + file + ". Error code: " + status);
106+
String actionString = (action == OPEN) ? "open"
107+
: (action == EDIT) ? "edit" : "print";
108+
throw new IOException("Failed to " + actionString + " " + file
109+
+ ". Error code: " + status);
81110
}
82111
}
83112

84-
private static native int _lsOpenURI(String uri);
113+
private static native int _lsOpenURI(String uri, int action);
85114

86-
private static native int _lsOpenFile(String path, boolean print);
115+
private static native int _lsOpenFile(String path, int action, String tmpTxtPath);
87116

88117
}

jdk/src/macosx/native/sun/awt/AWTWindow.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1495,6 +1495,7 @@ - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame {
14951495
int shieldLevel = CGShieldingWindowLevel();
14961496
window.preFullScreenLevel = [nsWindow level];
14971497
[nsWindow setLevel: shieldLevel];
1498+
[nsWindow makeKeyAndOrderFront: nil];
14981499

14991500
NSRect screenRect = [[nsWindow screen] frame];
15001501
[nsWindow setFrame:screenRect display:YES];

jdk/src/macosx/native/sun/awt/CDesktopPeer.m

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,27 +27,61 @@
2727
#import <CoreFoundation/CoreFoundation.h>
2828
#import <ApplicationServices/ApplicationServices.h>
2929
#import <JavaNativeFoundation/JavaNativeFoundation.h>
30+
#import <AppKit/AppKit.h>
31+
#import "sun_lwawt_macosx_CDesktopPeer.h"
3032

3133
/*
3234
* Class: sun_lwawt_macosx_CDesktopPeer
3335
* Method: _lsOpenURI
34-
* Signature: (Ljava/lang/String;)I;
36+
* Signature: (Ljava/lang/String;I)I
3537
*/
3638
JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CDesktopPeer__1lsOpenURI
37-
(JNIEnv *env, jclass clz, jstring uri)
39+
(JNIEnv *env, jclass clz, jstring uri, jint action)
3840
{
39-
OSStatus status = noErr;
41+
__block OSStatus status = noErr;
4042
JNF_COCOA_ENTER(env);
4143

42-
// I would love to use NSWorkspace here, but it's not thread safe. Why? I don't know.
43-
// So we use LaunchServices directly.
44-
45-
NSURL *url = [NSURL URLWithString:JNFJavaToNSString(env, uri)];
46-
47-
LSLaunchFlags flags = kLSLaunchDefaults;
48-
49-
LSApplicationParameters params = {0, flags, NULL, NULL, NULL, NULL, NULL};
50-
status = LSOpenURLsWithRole((CFArrayRef)[NSArray arrayWithObject:url], kLSRolesAll, NULL, &params, NULL, 0);
44+
NSURL *urlToOpen = [NSURL URLWithString:JNFJavaToNSString(env, uri)];
45+
NSURL *appURI = nil;
46+
47+
if (action == sun_lwawt_macosx_CDesktopPeer_BROWSE) {
48+
// To get the defaultBrowser
49+
NSURL *httpsURL = [NSURL URLWithString:@"https://"];
50+
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
51+
appURI = [workspace URLForApplicationToOpenURL:httpsURL];
52+
} else if (action == sun_lwawt_macosx_CDesktopPeer_MAIL) {
53+
// To get the default mailer
54+
NSURL *mailtoURL = [NSURL URLWithString:@"mailto://"];
55+
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
56+
appURI = [workspace URLForApplicationToOpenURL:mailtoURL];
57+
}
58+
59+
if (appURI == nil) {
60+
return -1;
61+
}
62+
63+
// Prepare NSOpenConfig object
64+
NSArray<NSURL *> *urls = @[urlToOpen];
65+
NSWorkspaceOpenConfiguration *configuration = [NSWorkspaceOpenConfiguration configuration];
66+
configuration.activates = YES; // To bring app to foreground
67+
configuration.promptsUserIfNeeded = YES; // To allow macOS desktop prompts
68+
69+
// dispatch semaphores used to wait for the completion handler to update and return status
70+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
71+
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC)); // 1 second timeout
72+
73+
// Asynchronous call to openURL
74+
[[NSWorkspace sharedWorkspace] openURLs:urls
75+
withApplicationAtURL:appURI
76+
configuration:configuration
77+
completionHandler:^(NSRunningApplication *app, NSError *error) {
78+
if (error) {
79+
status = (OSStatus) error.code;
80+
}
81+
dispatch_semaphore_signal(semaphore);
82+
}];
83+
84+
dispatch_semaphore_wait(semaphore, timeout);
5185

5286
JNF_COCOA_EXIT(env);
5387
return status;
@@ -56,30 +90,72 @@
5690
/*
5791
* Class: sun_lwawt_macosx_CDesktopPeer
5892
* Method: _lsOpenFile
59-
* Signature: (Ljava/lang/String;Z)I;
93+
* Signature: (Ljava/lang/String;I;Ljava/lang/String;)I;
6094
*/
6195
JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CDesktopPeer__1lsOpenFile
62-
(JNIEnv *env, jclass clz, jstring jpath, jboolean print)
96+
(JNIEnv *env, jclass clz, jstring jpath, jint action, jstring jtmpTxtPath)
6397
{
64-
OSStatus status = noErr;
98+
__block OSStatus status = noErr;
6599
JNF_COCOA_ENTER(env);
66100

67-
// I would love to use NSWorkspace here, but it's not thread safe. Why? I don't know.
68-
// So we use LaunchServices directly.
69-
70101
NSString *path = JNFNormalizedNSStringForPath(env, jpath);
71-
72-
NSURL *url = [NSURL fileURLWithPath:(NSString *)path];
102+
NSURL *urlToOpen = [NSURL fileURLWithPath:(NSString *)path];
73103

74104
// This byzantine workaround is necesary, or else directories won't open in Finder
75-
url = (NSURL *)CFURLCreateWithFileSystemPath(NULL, (CFStringRef)[url path], kCFURLPOSIXPathStyle, false);
76-
77-
LSLaunchFlags flags = kLSLaunchDefaults;
78-
if (print) flags |= kLSLaunchAndPrint;
79-
80-
LSApplicationParameters params = {0, flags, NULL, NULL, NULL, NULL, NULL};
81-
status = LSOpenURLsWithRole((CFArrayRef)[NSArray arrayWithObject:url], kLSRolesAll, NULL, &params, NULL, 0);
82-
[url release];
105+
urlToOpen = (NSURL *)CFURLCreateWithFileSystemPath(NULL, (CFStringRef)[urlToOpen path],
106+
kCFURLPOSIXPathStyle, false);
107+
108+
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
109+
NSURL *appURI = [workspace URLForApplicationToOpenURL:urlToOpen];
110+
NSURL *defaultTerminalApp = [workspace URLForApplicationToOpenURL:[NSURL URLWithString:@"file:///bin/sh"]];
111+
112+
// Prepare NSOpenConfig object
113+
NSArray<NSURL *> *urls = @[urlToOpen];
114+
NSWorkspaceOpenConfiguration *configuration = [NSWorkspaceOpenConfiguration configuration];
115+
configuration.activates = YES; // To bring app to foreground
116+
configuration.promptsUserIfNeeded = YES; // To allow macOS desktop prompts
117+
118+
// pre-checks for open/print/edit before calling openURLs API
119+
if (action == sun_lwawt_macosx_CDesktopPeer_OPEN
120+
|| action == sun_lwawt_macosx_CDesktopPeer_PRINT) {
121+
if (appURI == nil
122+
|| [[urlToOpen absoluteString] containsString:[appURI absoluteString]]
123+
|| [[defaultTerminalApp absoluteString] containsString:[appURI absoluteString]]) {
124+
return -1;
125+
}
126+
// Additionally set forPrinting=TRUE for print
127+
if (action == sun_lwawt_macosx_CDesktopPeer_PRINT) {
128+
configuration.forPrinting = YES;
129+
}
130+
} else if (action == sun_lwawt_macosx_CDesktopPeer_EDIT) {
131+
if (appURI == nil
132+
|| [[urlToOpen absoluteString] containsString:[appURI absoluteString]]) {
133+
return -1;
134+
}
135+
// for EDIT: if (defaultApp = TerminalApp) then set appURI = DefaultTextEditor
136+
if ([[defaultTerminalApp absoluteString] containsString:[appURI absoluteString]]) {
137+
NSString *path = JNFNormalizedNSStringForPath(env, jtmpTxtPath);
138+
NSURL *tempFilePath = [NSURL fileURLWithPath:(NSString *)path];
139+
appURI = [workspace URLForApplicationToOpenURL:tempFilePath];
140+
}
141+
}
142+
143+
// dispatch semaphores used to wait for the completion handler to update and return status
144+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
145+
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC)); // 1 second timeout
146+
147+
// Asynchronous call - openURLs:withApplicationAtURL
148+
[[NSWorkspace sharedWorkspace] openURLs:urls
149+
withApplicationAtURL:appURI
150+
configuration:configuration
151+
completionHandler:^(NSRunningApplication *app, NSError *error) {
152+
if (error) {
153+
status = (OSStatus) error.code;
154+
}
155+
dispatch_semaphore_signal(semaphore);
156+
}];
157+
158+
dispatch_semaphore_wait(semaphore, timeout);
83159

84160
JNF_COCOA_EXIT(env);
85161
return status;

jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -93,6 +93,13 @@ public Soundbank getSoundbank(AudioInputStream ais)
9393
"Can not allocate enough memory to read audio data.");
9494
}
9595

96+
long maximumHeapSize = (long) ((Runtime.getRuntime().maxMemory() -
97+
(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())) * 0.9);
98+
if (totalSize > maximumHeapSize) {
99+
throw new InvalidMidiDataException(
100+
"Insufficient heap size to render audio data.");
101+
}
102+
96103
if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) {
97104
ByteArrayOutputStream baos = new ByteArrayOutputStream();
98105
byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)];

jdk/src/share/classes/com/sun/net/httpserver/Headers.java

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.sun.net.httpserver;
2727

2828
import java.util.*;
29+
import sun.net.httpserver.Utils;
2930

3031
/**
3132
* HTTP request and response headers are represented by this class which implements
@@ -129,8 +130,14 @@ public String getFirst (String key) {
129130
}
130131

131132
public List<String> put(String key, List<String> value) {
133+
// checkHeader is called in this class to fail fast
134+
// It also must be called in sendResponseHeaders because
135+
// Headers instances internal state can be modified
136+
// external to these methods.
137+
Utils.checkHeader(key, false);
138+
132139
for (String v : value)
133-
checkValue(v);
140+
Utils.checkHeader(v, true);
134141
return map.put (normalize(key), value);
135142
}
136143

@@ -142,7 +149,8 @@ public List<String> put(String key, List<String> value) {
142149
* @param value the header value to add to the header
143150
*/
144151
public void add (String key, String value) {
145-
checkValue(value);
152+
Utils.checkHeader(key, false);
153+
Utils.checkHeader(value, true);
146154
String k = normalize(key);
147155
List<String> l = map.get(k);
148156
if (l == null) {
@@ -152,30 +160,6 @@ public void add (String key, String value) {
152160
l.add (value);
153161
}
154162

155-
private static void checkValue(String value) {
156-
int len = value.length();
157-
for (int i=0; i<len; i++) {
158-
char c = value.charAt(i);
159-
if (c == '\r') {
160-
// is allowed if it is followed by \n and a whitespace char
161-
if (i >= len - 2) {
162-
throw new IllegalArgumentException("Illegal CR found in header");
163-
}
164-
char c1 = value.charAt(i+1);
165-
char c2 = value.charAt(i+2);
166-
if (c1 != '\n') {
167-
throw new IllegalArgumentException("Illegal char found after CR in header");
168-
}
169-
if (c2 != ' ' && c2 != '\t') {
170-
throw new IllegalArgumentException("No whitespace found after CRLF in header");
171-
}
172-
i+=2;
173-
} else if (c == '\n') {
174-
throw new IllegalArgumentException("Illegal LF found in header");
175-
}
176-
}
177-
}
178-
179163
/**
180164
* sets the given value as the sole header value
181165
* for the given key. If the mapping does not

0 commit comments

Comments
 (0)